Confused with how transform.Translate() takes in parameters

public class PlayerMovement : MonoBehaviour {

    Vector2 up;

    void Start () {
        up = new Vector2(0,1) * 0.05f;
    }
   
    void Update () {

        transform.Translate(0,1,1);

        transform.Translate(0, 1);  //Why doesn't this work? Isn't it the same as below?
        transform.Translate(up);
       
    }
}

The line of code with comments displays an error in Visual Studio, but the one below it does not. Confused because I thought Translate needs Vector3, but up is Vector2.

it just dosnt have a overload for taking 2 floats as a arg, and in the case where you fed it 3 floats behind the scenes it is likely just converting that to a Vector3

feeding it your Vector2 up works because unity does a implicit cast between Vector2 to Vector3.

also it is likely best practice to always use vectors with translate as opposed to floats.

1 Like

The Vector2 gets implicitly converted to Vector3 (with z = 0). If you look at the docs, you see Translate does not have any overload for using 2 floats like (0, 1). You either need a Vector3, or else 3 floats.

–Eric

1 Like

Watch my tutorial series on player movement.