Why Using transform.localposition insted of Translate()

I have a question,

to my eyes, I see no diference using Transform.Translate() instead of transfor. localPosition

private void ShipMovement()

{

    float horizontalMovement = Input.GetAxis("Horizontal") * Time.deltaTime * speedMovement;

    float verticalMovement = Input.GetAxis("Vertical") * Time.deltaTime * speedMovement;

    transform.Translate(horizontalMovement, verticalMovement, 0);

}

So what is the difference or Maybe is the same?

(ofcourse using in the child prefab has no effect to the parent so I have the question

Context is always important.

Without a parent, transform.position and transform.localPosition is for the most part the same. If there is a parent and the parent is moving, it gives you a different result. The same as transform.Translate is the same until the context changes, in which, it can give you a different result.

1 Like

Thank you, I Noticed with transform.translated() when I wanted to clamp my variables, it didn´t work, so I used transform.localPosition and worked.

Thank you a lot

You can clamp any mentioned above and they should work in a similar fashion. It does depend on how you’re clamping it. When you’re adding Time.deltaTime into the mix, the numbers get rather small. That means you have to clamp the result. That may be why you’re finding a difference between the two meodologies.

One is essentially transform.position = newPosition;
where the other essentially is transform.position += positionOffset;

So if you’re clamping the Vector3 you’re entering into the equation, newPosition might be something like (10,5,25) where the positionOffset might be something like (0.005, 0.003, 0.008). Your expectation with the clamping might not match with how it’s working.

Does that make sense?

One thing I didn’t mention, Translate keeps colliders in mind, where transform.position/localPosition doesn’t.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms