- Home /
Play animation for horizontal and vertical movement?
Hey,
i did some research but did not find a clear answer, and when I had something I hard problems applying it to my script. What I want to do is play an animation when my character (first person) is moving forward and backward and a different one when he is moving left and right. So basically is there a way I can check for horizontal movement and vertical movement and then apply a specific animation to that movement.
If it helps here is my code for the movement of the character.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(CharacterController))]
public class FirstPersonController : MonoBehaviour {
public float movementSpeed = 5.0f;
public float mouseSensitivity = 2.0f;
public float upDownRange = 60.0f;
public float jumpSpeed = 3.0f;
bool sprintCoolDown = false;
float desiredUpDown = 90;
float verticalVelocity = 0;
CharacterController characterController;
// Use this for initialization
void Start () {
Screen.lockCursor = true;
characterController = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
// Rotation
float rotLeftRight = Input.GetAxis("Mouse X") * mouseSensitivity;
transform.Rotate(0 , rotLeftRight , 0);
desiredUpDown -= Input.GetAxis("Mouse Y") * mouseSensitivity;
desiredUpDown = Mathf.Clamp(desiredUpDown, -upDownRange , upDownRange);
Camera.main.transform.localRotation = Quaternion.Euler (desiredUpDown , 0 , 0 );
// Movement of Character
if(Input.GetKey(KeyCode.LeftShift) && sprintCoolDown == false) {
movementSpeed = 8.0f;
StartCoroutine(sprintCD());
}
else {
movementSpeed = 5.0f;
}
float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;
verticalVelocity += Physics.gravity.y * Time.deltaTime;
if( characterController.isGrounded && Input.GetButtonDown("Jump") ) {
verticalVelocity = jumpSpeed;
}
Vector3 speed = new Vector3 ( sideSpeed , verticalVelocity , forwardSpeed );
speed = transform.rotation * speed;
characterController.Move( speed * Time.deltaTime );
}
IEnumerator sprintCD() {
yield return new WaitForSeconds(6);
sprintCoolDown = true;
yield return new WaitForSeconds(4);
sprintCoolDown = false;
}
}
Any help is appreciated. Thanks a lot. Kind Regards Cribbel (Still a fair noob at unity but getting the hang of it)
Answer by LukaKotar · Dec 13, 2013 at 05:20 PM
By default, Unity has input controls set up for 'Horizontal' and 'Vertical'. You can see them by going into Edit>Project Settings>Input. There will be four fields: 'Positive Button', 'Negative Button', and the 'Alt Positive Button' and 'Alt Negative Button'.
So, vertical input would be W or S, or the up and down arrow keys. W and arrow up are positive, so they will return 1, while S and arrow down, will return -1. If none of those is pressed, it will return 0. The same is for the horizontal input.
Here is how you would play different animations according to which buttons are pressed:
void Update(){
//If the player is moving horizontally (left and right), and not diagonally or vertically
//Remember: 1 is right (positive), and -1 is left (negative).
if(Input.GetAxis("Horizontal") != 0 && Input.GetAxis("Vertical") == 0) {
//Play your sideways animation
animation.Play("Insert animation name here");
}
//If the player is moving vertically (forward and backward) or diagonally
else {
//Play your forward/backward animation
animation.Play("Insert animation name here");
}
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How can I play different animations depending on the direction my character is moving? 1 Answer
Mixing Unity Mecanim and Scripted Bones/States 1 Answer
How to use 'check if player is on ground' bool in script 2 Answers
Get walk animation to play on horizontal and vertical axis presses. 2 Answers