Search code examples
unity-game-enginegame-physics

Best way to move a game object in Unity 3D


I'm going through a few different Unity tutorials and the way a game object is moved around in each is a little different.

What are the pros/cons to each of these methods and which is preferred for a first person RPG?

// Here I use MovePosition function on the rigid body of this component
Rigidbody.MovePosition(m_Rigidbody.position + movement);

//Here I apply force to the rigid body and am able to choose force mode
Rigidbody.AddForce(15 * Time.deltaTime, 0, 0, ForceMode.VelocityChange);

// Here I directly change a transforms position value, in this case the cam
Transform.transform.position = playerTransform.position + cameraOffset;

Thanks!!

EDIT;

Something I have noticed is that the applied force seems to memic wheeled vehicles while the position changes memic walking/running.


Solution

  • RigidBodies and Velocities/Physics

    The only time, I personally have used the rigidbodys system was when implementing my own boids (flocking behaviour) as you need to calculate a few separate vectors and apply them all to the unit.

       Rigidbody.MovePosition(m_Rigidbody.position + movement);
    

    This calculates a movement vector towards a target for you using the physics system, so the object's velocity and movement can still be affected by drag, angular drag and so on.

    This particular function is a wrapper around Rigidbody.AddForce I believe.

    Pros :

    • Good if realistic physical reactions is something you are going for

    Cons:

    • A bit unwieldy to use if all you are trying to achieve is moving a object from point A to point B.

      • Sometimes an errant setting set too high somewhere (for example: Mass > 10000000) can cause really screwy bugs in behaviour that can be quite a pain to pin down and mitigate.

    Notes: Rigidbodies when colliding with another Rigidbody would bounce from each other depending on physics settings.

    They are also affected by gravity. Basically they try to mimic real life objects but it can be sometimes difficult to tame the objects and make them do exactly what you want.

    And Rigidbody.AddForce is basically the same as above except you calculate the vector yourself.

    So for example to get a vector towards a target you would do

    Vector3 target = target.position - myPosition;
    
    Rigidbody.AddForce(target * 15 * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    

    If you don't plan on having any major physics mechanics in your game, I would suggest moving by interpolating the objects position.

    As it is far easier to get things to behave how you want, unless of course you are going for physical realism!

    Interpolating the units position

    Pros :

    • Perhaps a little strange to understand at first but far simpler to make objects move how you want

    Cons:

    • If you wanted realistic reactions to objects impacting you'd have to do a lot of the work yourself. But sometimes this is preferable to using a physics system then trying, as I've said earlier to tame it.

    You would use the technique in say a Pokemon game, you don't stop in Pokemon and wait for ash to stop skidding or hit a wall and bounce uncontrollably backwards.

    This particular function is setting the objects position like teleporting but you can also use this to move the character smoothly to a position. I suggest looking up 'tweens' for smoothly interpolating between variables.

        //change the characters x by + 1 every tick, 
    Transform.transform.position.x += 1f;