I’m just playing around with animations and now confronted a problem, when I want to move an object several times with the an animation.
I already made my play object with the animations a child of another empty GameObject.
I instantiate my player objacte in code and not in editor and the first animation works fine. The player (cube) spawns on the middle field and I am able to move it one field left, down, etc.
But when want to make another move the cube spawns at the center of the grid again and starts the animation from there.
I created 4 animation so the cube will rotate by 90° and also move by 1 in one of the 4 directions. Here is the code:
K, so the animation you where doing wasn’t right, because it was not looping, so it would jump when you turned around. So I went with the RotateAround solution.
I have it working, however you need your rigidbody to be isKinematic. So this is the code I ended up using:
private bool isMoving = false;
private int Direction = 0;
private Vector3 rotatePosition;
private static Vector3 originalRotation = new Vector3(90,0,0);
void Update() {
if(!isMoving)
{
if(Input.GetKeyDown("w")){
isMoving = true; //disable input while I move
Direction = 2;//I want to move upwards
//Calculate where do I need to rotate the object form
rotatePosition = transform.position;
rotatePosition.z+=0.5f;
rotatePosition.y-=0.5f;
}
if(Input.GetKeyDown("s")){
isMoving = true; //disable input while I move
Direction = 1;//I want to move downwards
//Calculate where do I need to rotate the object form
rotatePosition = transform.position;
rotatePosition.z-=0.5f;
rotatePosition.y-=0.5f;
}
if(Input.GetKeyDown("a")){
isMoving = true; //disable input while I move
Direction = 3;//I want to move left
//Calculate where do I need to rotate the object form
rotatePosition = transform.position;
rotatePosition.x-=0.5f;
rotatePosition.y-=0.5f;
}
if(Input.GetKeyDown("d")){
isMoving = true; //disable input while I move
Direction = 4;//I want to move right
//Calculate where do I need to rotate the object form
rotatePosition = transform.position;
rotatePosition.x+=0.5f;
rotatePosition.y-=0.5f;
}
}else{ //animating
switch(Direction)
{
case 1: //move upwards
transform.RotateAround(rotatePosition, Vector3.right, - 40 * Time.deltaTime);
break;
case 2: // down
transform.RotateAround(rotatePosition, Vector3.right, + 40 * Time.deltaTime);
break;
case 3: //left
transform.RotateAround(rotatePosition, Vector3.forward, + 40 * Time.deltaTime);
break;
case 4: //right
transform.RotateAround(rotatePosition, Vector3.forward, - 40 * Time.deltaTime);
break;
}
if(transform.localEulerAngles.x>180) //when I turn 90 degree this value will become or more then 180 or less then 0 which will result in 359. so in both case it's greated then 180
{
isMoving = false; //disable animation and give back control for input
transform.rotation=Quaternion.Euler(originalRotation); //set back the original rotation ( this works here because your every sight is the same, I do this to have easier if statemen when is my rotation over
Vector3 roundedPos = new Vector3(Mathf.Round(transform.position.x),
Mathf.Round(transform.position.y)+0.5f,
Mathf.Round(transform.position.z)); // i round off the current position, so I would have accurate rotation even when moving far form origin
transform.position = roundedPos; //save the rounded position back
}
}
}