Nero
Nero

Reputation: 11

Player movement overriding external forces/velocities from other gameobjects

I am trying to add an external velocity (from an external gameObject) onto my character. The problem is when the force is being added in combination with sending movementinput to the character the external force gets "denied" and only gets called one frame (i assume).

This is because in the player movement script i use a specific function to set the movement, and it is redefining (by setting velocity to maxMovementSpeed) the velocity every time unity gets movement input. Below is the movement function which sets the movement in the x-axis.

    private void SetMovement()
    {
        Vector3 vel = _rb2d.velocity;

        if(_direction == Direction.Right)
        {
            _speed = maxMovementSpeed;            
        }
        else if (_direction == Direction.Left)
        {
            _speed = -maxMovementSpeed;            
        }

        vel.x = _speed;

        if(_direction != Direction.Idle)
        {
            _rb2d.velocity = vel;
        }
    }

So again, in my case i would like to add a velocity "bump" or force from a projectile. So for example, depending on its direction, the character would be able to jump onto it, and get a boost in Y axis to reach higher altitudes! If it would be needed to look at the external force from the other object, the code is below.

    void OnCollisionEnter2D(Collision2D collision)
    {

        if (collision.gameObject.name == "Vagrant")
        {
            // VARIANT ETT \\
            collision.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
            collision.gameObject.GetComponent<Rigidbody2D>().velocity += projMovement.rb.velocity.normalized * boost;
        }
    }

So the problem is i want to get a "forceboost" from my projectile, and it gets called, and the boost works. But when input gets recognised it just sets the velocity directly to something around 60 (instead of my boost which is +150 etc). So the boost works when you stand still and the projectile hits you.

The question is, is there a simple way to fix/change this to a better way to handle movement? By not directly setting a fixed value when for example running in the x-axis. And i cannot write += instead of only =, since i dont want the "constant" to add/accelerate if that makes sense.

Upvotes: 0

Views: 817

Answers (1)

Martin Gonzalez
Martin Gonzalez

Reputation: 165

I recommend you to add a function in your Player that is AddForce and it will add the force. Do not modify values from another script from the outside, in that way you can control always how player react from the player script.

Now talking about the issue

Do not modify velocity value from rigidbody, use addForce method feom it (Rigidbody) giving positive or negative values! When you use the method addForce it use internally the current value of the rigidbody velocity so you dont habe any unexpected behaviour

Upvotes: 1

Related Questions