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)