- Home /
Move cube forward
So, my cube currently has animations for my cube in each direction (forward, back, left, right) in Unity4.
They currently don't move though, so what I want to happen is the player should move forward 1 unit every time the animation is played. How do I do this? My current C# script can be seen below
(Can the movement be done in animation?)
using UnityEngine; using System.Collections;
public class PlayerMovement : MonoBehaviour {
//Uses animations based on input and if no key is pushed then you stay idle
void Update() {
if(Input.GetKey("w"))
{
animation.Play("CubeMovementForward");
}
else if(Input.GetKey("a"))
{
animation.Play("CubeMovementLeft");
}
else if(Input.GetKey("s"))
{
animation.Play("CubeMovementBackward");
}
else if(Input.GetKey("d"))
{
animation.Play("CubeMovementRight");
}
else if(Input.GetButtonDown("Jump"))
{
animation.Play("CubeMovementJump");
}
}
}
Try using function transform.Translate ins$$anonymous$$d of animations. Look it up in the Unity Script $$anonymous$$anual Doc and try the Example Code. Or Itween for more flexible purpose.
Your answer
Follow this Question
Related Questions
Mecanim: change the animation even though it is not finish in playing 2 Answers
Can't Play Animation Clip When More Than One Clip Is Added To Animation 1 Answer
Walking Animation Feet Shimmy When Key Unpress 1 Answer
Addititive Animation Not Responding to Speed 1 Answer
pausing an animation, then running a different animation, and then resuming old animation 0 Answers