recently began to use yunku. What is the difference between transform.position and transform.Translate? in both cases, the character moves. what is better to use?

    1 answer 1

    According to the documentation for Transform.Translate () , the method takes an optional parameter Space relativeTo = Space.Self , which determines the vector relative to which the object will move.

    You can see the differences if you create three objects on the scene, to each of which you attach a corresponding script:

    1. PositionMovement - moves the object, changing the transform.position :

       void Update () { transform.Rotate(Vector3.up * 10f * Time.deltaTime); transform.position += Vector3.forward * Time.deltaTime; } 
    2. TranslateSelfMovement - moves the object by calling the transform.Translate method with the Space.Self parameter (used by default if omitted)

       void Update () { transform.Rotate(Vector3.up * 10f * Time.deltaTime); transform.Translate(Vector3.forward * Time.deltaTime); } 
    3. TranslateWorldMovement - moves the object by calling the transform.Translate method with the Space.World parameter:

       void Update () { transform.Rotate(Vector3.up * 10f * Time.deltaTime); transform.Translate(Vector3.forward * Time.deltaTime, Space.World); } 

    As you can see, each object additionally rotates around a vertical axis.

    By running this example, you will see that one object describes circles (with the TranslateSelfMovement script), while the other two are moving forward.

    unity_1

    If you slightly adjust each script, replacing Vector3.forward with transform.forward , then all objects will describe circles, but the object with the TranslateSelfMovement script will have a different radius of circle:

    unity_2