- Home /
smoothly move an object from one vector to another (crashing unity)
I'm trying to move an object from one point to another (it's important that the numbers increment exactly), I want the object to move there smoothly, though. here's the code I used, Unity crashed when I tried it:
public float XPos = 2;
public Vector2 playerPos = new Vector2 (2, 2);
void Update ()
{
playerPos = new Vector2 (XPos, 2);
//Mathf.Round (XPos);
transform.position = playerPos;
if(Input.GetKeyDown (KeyCode.A))
{
Move();
}
}
void Move ()
{
float i = XPos + 1;
while (XPos < i )
{
transform.Translate(Vector3.up * Time.deltaTime);
}
}
Answer by Graham-Dunnett · Jun 07, 2014 at 09:36 PM
Unity did not crash. Your while loop at line 17 is stuck in an infinite loop. The body of the loop doesn't change either the XPos or i variables, so the test you make never changes to false.
Your answer
Follow this Question
Related Questions
Why wont my object move towards the desired location? 2 Answers
One Key press to trigger .MoveTowards to go through its whole movement. 2 Answers
How do i make a cube "slide"and fixed on a rail? 0 Answers
How can I move an object to a clicked objects (center of X,Z) coordinates (board game)? 0 Answers
returning a relative Vector? 2 Answers