Set Vector3 position using local position and rounded coordinates

Hi

I have a 3D game with a object which uses Transform.Translate to move from one side of the scene to the other. Now as im moving this in a FixedUpdate sometime the object doesnt stop on a rounded number e.g. it will stop on X (0.933) when i want to to stop on X(1). I can use iTween etc as im using Physics.

So i thought about using Get and Set Position but how do i round the number and then set the local Vector3 in local space and not world space?

Hope im making sense.

Nick

is Mathf.Round, Mathf.Ceil or Mathf.Floor what you are looking for ?
To convert a world position to a local position, you can use transform.InverseTransformPoint()

You can use Mathf.Round:

Quick tip:

If your transform use a rigidbody, don’t use Transform.translate but Rigidbody.MovePosition.
If you use Transform.translate it will only detect any collision the next frame.

Hi Fabian-Haquin

Ok yes The ‘Mathf Ceil’ is what im looking for, i need to have the object snap to the nearest rounded number
so if 0.99 would be 1 and if 1.02 would be 1. So you are saying that if i wanted to round one Axis i would need to use transform.InverseTransformPoint() and then sett the ‘world position’ of the object?

I feel i need to explain in more depth as to how my player object moves. Basically i have a 3D game where the player can move (1) on the local Z axis when they press a button, of course the player doesn’t land on (1) all the time. If the player holds the button down they will keep moving another (1). So what im trying to do is before each iteration/loop in the code i want to make sure the player has not gone too far / or to short to the round number on its local Z axis. So think of it as a grid snap style game but with physics (i can’t move away from physics for this).

p.s. thanks for the tip, greatly appreciated :slight_smile:

Hi fire7side,
Thanks for this.

Nick