Can someone tell me the difference between AddForce and transform.translate?

I am trying to move a gameobject but I found these two most popular functions to move the object. The difference I know between these two is that there is a rigidbody needed in using AddForce but no Rigidbody needed in Transform.translate. I just wanted to make sure that I am using right function for game. Is there anny difference in the functionality of these two function? I am moving a cube over an infinite plane with obstacles in the way. I think rigidbody is needed ofcourse but I want to know what is the use of Transform.translate?

2 answers

  • answered 2019-03-13 17:29 Berme

    transform.translate:

    With this method you basically teletransport the GameObject, not taking in account physics nor colliders. This one is pretty expensive if you have a RigidBody attached.

    AddForce:

    With this one you are adding force to the RigidBody of the GameObject, so all the movement will be taking physics and colliders into account.

    If you need to move a RigidBody (like a Player) I recommend using MovePosition. It's more precise than AddForce and use the physics motor.

    EDIT:

    Example:

    public float movementSpeed= 5f; //for instance
    
    void FixedUpdate()
    {
    
        Vector3 direction = new Vector3(Input.GetAxisRaw("Horizontal"), 0 , Input.GetAxisRaw("Vertical"));
        rigidbody.MovePosition(transform.position + direction * movementSpeed * Time.fixedDeltaTime);
    }
    

    Tweak movementSpeed to go faster or slower.

    Didn't test the code but it should work.

  • answered 2019-03-13 17:30 AminSojoudi

    Sometimes you don't want to use physics in your game cause it uses a lot of calculating power, also there are a lot of other use cases that the developer does not need to use physics to move the object or the collision with other objects is not important.