- Home /
why one works, the other does not
why would one of these work (hit key E, it goes to position B) but the other does not (hit key Q, doesn't go to position A)? thanks for any ideas/helps
{
Vector3 positionA = new Vector3 (-45, 15, 0);
Vector3 positionB = new Vector3 (-1, 4, 8);
if (Input.GetKey (KeyCode.Q))
newPosition = positionA;
else {
newPosition = transform.position;
}
if (Input.GetKey (KeyCode.E))
newPosition = positionB;
else{
newPosition = transform.position;
}
transform.position = Vector3.Lerp (transform.position, newPosition, smooth * Time.deltaTime);
}
thanks vexe! that put me on the right track- many thanks
converted to answer. give it the tick if it addressed your problem
Answer by vexe · Feb 23, 2015 at 09:23 PM
It's probably related to your if-else logic. Put some logs in there and see what's getting executed.
Try this (pseudo):
newPosition = transform.position;
if (Q) newPosition = A;
else if (E) = newPosition = B;
there's a redundant assignment but hey, if it works then you'll know there's a problem with you if-else
Answer by maccabbe · Feb 24, 2015 at 01:52 AM
The reason for this is because of your if/else logic. If Q is hit then you correctly assign newPosition = positionA. But then you always reassign to newPosition afterwords in lines 10-14 so newPosition does not remain assigned to positionA.
Your answer
Follow this Question
Related Questions
Turn and Move Foward 1 Unit relative to Objects local direction 1 Answer
How to determine speed of an object when using transform.position 2 Answers
Move camera when mouse is near the edges of the screen 1 Answer
How to make object move smoothly. 3 Answers
Vector3.Lerp completing before t = 1? 2 Answers