I am working on a game and I am getting this issue with my game object’s position. What I have is a game object (space ship) and I used the Animation Window to set up two animations (idle and slightly rotating forward).
My situation is that I have the ship moving forward, but when I release the button instead of the ship stopping where it should, it goes back to where it first started. Can anyone help me figure out what I am doing wrong?
Here is my code:
enum ShipStates { idle, forward, left, right }
ShipStates CurrentState = ShipStates.idle;
Vector3 PreviousPosition,
CurrentPosition;
void Start()
{
PreviousPosition = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
}
// Update is called once per frame
void Update ()
{
switch (CurrentState)
{
case ShipStates.idle:
animation.Play("Idle");
PreviousPosition = CurrentPosition;
break;
case ShipStates.forward:
animation.Play("Forward");
gameObject.transform.Translate(Vector3.forward * Time.deltaTime);
CurrentPosition = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
break;
case ShipStates.left:
break;
case ShipStates.right:
break;
}
}
And many thanks in advance!