- Home /
 
How to predict position of object before transform.translate will apply?
I use transform translate to move object depending on the parent transform position and rotation and I need to check boundary position. So, what is the way to predict position of object before transform.position will apply. Because now I use this:
 public void MoveZ(int dir)
     {
         Vector3 newPos;
         transform.Translate(Vector3.forward * dir * step * Time.deltaTime);
         newPos = transform.localPosition;
         transform.Translate(Vector3.forward * -dir * step * Time.deltaTime);
         if (newPos.z <= maxMoveZ && newPos.z >= minMoveZ) transform.Translate(Vector3.forward * dir * step * Time.deltaTime);
     }
 
               I move object, then remember it position and move object back, after that I check limits. So if position is correct I again move object to those position.
Answer by AaronXRDev · Nov 13, 2018 at 05:15 PM
You could just add the Vector3 of the position and test it that way. Something like this:
 public void MoveZ(int dir)
      {
          Vector3 newPos = transform.position + (Vector3.forward * dir * step * Time.deltaTime);
          if (newPos.z <= maxMoveZ && newPos.z >= minMoveZ) transform.Translate(Vector3.forward * dir * step * Time.deltaTime);
      }
 
              Your answer
 
             Follow this Question
Related Questions
Position and rotate 3 sprites perpendicular to the edges of a triangle 1 Answer
how to ignore transform.position.y 3 Answers
Getting a Vector2 position depending on the rotation of an object 0 Answers
make player move in direction it's facing 2 Answers
Dropping an object behind player in respect to rotation,How to drop an object behind the player 1 Answer