- Home /
Continously move an object with animation
Hi guys,
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:
if(Input.GetKeyDown("w")){
animation.Play("move_forward", PlayMode.StopAll);
transform.Translate(Vector3.forward, Space.World);
}
When commenting the animation out, I can coninously move around the whole field. But the nice rotation is missing.
What would be the best solution for this? Can I fix the animation somehow or should I try to rotate the cube in code? (which I couldn't achieve yet).
It seems I have to translate the parent object and animate the child? Is this right? I split the code 2 two scripts and applied the translation to the parent. Now i have the problem that both happens at the sime time -> translate happens first and then the animation is started. The behaviour would be right if it wouldn't skip one.
you can reference from your script the parent object. transform.parent.transform.Translate etc. So in your example use:
if(Input.Get$$anonymous$$eyDown("w")){
animation.Play("move_forward", Play$$anonymous$$ode.StopAll);
transform.parent.transform.Translate(Vector3.forward, Space.World);
}
Okay then the result is still the same, but only with one script ;) First jump to Translate position, then starts the animation from there. I would need the translation at the end of the animation. I read something with yield, but couldn't make it work
Answer by gajdot · Nov 20, 2013 at 02:54 PM
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
}
}
}
yield WaitForAnimation( animation );
This line is not working for me. Where do I have to call is StatCoroutine stuff? In start? And how do I handle inputs then? ANd where would I cann the moveUp method from your code?
I made the event thing running. Before my unity crashed once. But now the cube just moves one field further after the animation. Still not the expected outcome :D
You should have a code like this:
if(Input.Get$$anonymous$$eyDown("w")){
StartCoroutine(moveUp);
}
isn't this workking?
Oh yeah now the code is working. But still it's moving two fields and gets resettet to center T_T I guess there is anothe rproblem I'm not thinking of :/
$$anonymous$$ake a test project and post it please, so I could check what's the problem. I'll fix it and post it back.