1
votes

I'm trying to use the .AddForce code alongside the .movePosition one, but the .movePosition making the .AddForce do nothing. I think is because the line: " Vector2 movement = new Vector2 (moveHorizontal, 0);" but I really don't know how to fix it. The .AddForce code works as inteded by itself.

Edit: posted full code.

using UnityEngine; using System.Collections;

public class CompletePlayerController : MonoBehaviour {

public float speed;                //Floating point variable to store the player's movement speed.
public float jumpforce;
private Rigidbody2D rb2d;        //Store a reference to the Rigidbody2D component required to use 2D Physics.

// Use this for initialization
void Start()
{
    //Get and store a reference to the Rigidbody2D component so that we can access it.
    rb2d = GetComponent<Rigidbody2D> ();
}


//FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
void FixedUpdate()
{
    Jump();

    //Store the current horizontal input in the float moveHorizontal.
    float moveHorizontal = Input.GetAxis ("Horizontal");

    //Store the current vertical input in the float moveVertical.

    //Use the two store floats to create a new Vector2 variable movement.
    Vector2 movement = new Vector2 (moveHorizontal, 0);

    //Call the AddForce function of our Rigidbody2D rb2d supplying movement multiplied by speed to move our player.
    rb2d.MovePosition ((Vector2)transform.position + (movement * speed * Time.deltaTime));

}
void Jump(){
    if (Input.GetButtonDown("Vertical")){
    rb2d.AddForce(new Vector2(0, jumpforce), ForceMode2D.Impulse);
    }
}

}

1
You don't even show the AddForce code here. AddForce will apply a force to an object where as MovePosition will move the object using interpolation. Seems like MovePosition will simply overwrite the applied forceDerek C.
Oh, yes sorry. Will edit it now. Thanks for replying! The answer is helpful thanks!Juan Franco Lagar

1 Answers

1
votes

As the names say:

MovePosition

Moves the rigidbody to the specified position by calculating the appropriate linear velocity required to move the rigidbody to that position during the next physics update. During the move, neither gravity or linear drag will affect the body.

AddForce

Adds a force to the Rigidbody

The force is specified as two separate components in the X and Y directions (there is no Z direction in 2D physics). The object will be accelerated by the force according to the law force = mass x acceleration - the larger the mass, the greater the force required to accelerate to a given speed.

So according to the mass and friction of the object it causes it to move with a certain velocity according to the physics.


Mixing both makes not much sense since MovePosition is usually used for isKinematic rigidbodies, so ones that are fully controlled via script while AddForce only has effect on not isKinatic ones, thus fully controlled by physics alone; by mixing both you don't give AddForce a chance to work since you overwrite the linear velocity of the rigodbody by using MovePosition which adjusts an absolute velocity to the Rigidbody thus completely overruling the AddForce.

rb2d.MovePosition (transform.position + (movement * speed * Time.deltaTime));

this target position you want it to move towards has always the same Y as transform.position.y so you basically nail your object to that height.


However, if you think about it, all physics actions have one thing in common: In the end they are all different ways to under the hood apply a certain velocity to the rigodbody.

So instead of letting these methods calculate that velocity you could instead rather directly change the velocity yourself and thereby let AddForce fully control the Y velocity while you control only X component:

rb2d.velocity = new Vector2(movement * speed, rb2d.velocity.y);