- Home /
How do I add animation to a top-down 2d movement script?
I'm sure this has been asked before, I just don't know how to do it myself. I want to have an animation play when going in each direction as well as a unique animation for when the "run" button is held. I have three animations, How would I do it? Here's the code.
public class Move : MonoBehaviour { //this is the players movement, thanks internet public float speed = 3f; void Update() { Vector3 pos = transform.position; if (Input.GetKey("w")) { pos.y += speed * Time.deltaTime; } if (Input.GetKey("s")) { pos.y -= speed * Time.deltaTime; } if (Input.GetKey("d")) { pos.x += speed * Time.deltaTime; } if (Input.GetKey("a")) { pos.x -= speed * Time.deltaTime; } transform.position = pos; // this is a script for the run button if (Input.GetKeyDown(KeyCode.RightShift)) { speed = speed + 3; } if (Input.GetKeyUp(KeyCode.RightShift)) { speed = speed - 3; } } }
I'm just starting to learn, so any other advice would be appreciated.
Your answer
