Search Unity

Set Vector3 position using local position and rounded coordinates

Discussion in 'Scripting' started by coxy17, Oct 11, 2017.

  1. coxy17

    coxy17

    Joined:
    Sep 27, 2013
    Posts:
    116
    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
     
  2. Fabian-Haquin

    Fabian-Haquin

    Joined:
    Dec 3, 2012
    Posts:
    230
  3. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
  4. Fabian-Haquin

    Fabian-Haquin

    Joined:
    Dec 3, 2012
    Posts:
    230
    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.
     
  5. coxy17

    coxy17

    Joined:
    Sep 27, 2013
    Posts:
    116
    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 :)

    Hi fire7side,
    Thanks for this.

    Nick
     
unityunity