Reputation: 21
I have a problem where the player moves in the direction but the animation of the charcter stays the same. So if i click "w" key the player runs forward the animation works. But when i click "s" for backwards the character does not rotate around to the direction it just moves/slides back with the character facing forward and no animation. Please help!!
public class Player : MonoBehaviour {
private Animator anim;
private CharacterController controller;
public float speed = 600.0f;
public float turnSpeed = 400.0f;
private Vector3 moveDirection = Vector3.zero;
public float gravity = 20.0f;
private Vector3 curLoc;
void Start () {
controller = GetComponent <CharacterController>();
anim = gameObject.GetComponentInChildren<Animator>();
}
void Update (){
if (Input.GetKey ("w")) {
anim.SetInteger ("AnimationPar", 1);
} else {
anim.SetInteger ("AnimationPar", 0);
}
if (Input.GetKey("s"))
{
anim.SetInteger("Condition", 1);
}
else
{
anim.SetInteger("Condition", 0);
}
if (controller.isGrounded){
moveDirection = transform.forward * Input.GetAxis("Vertical") * speed;
}
float turn = Input.GetAxis("Horizontal");
transform.Rotate(0, turn * turnSpeed * Time.deltaTime, 0);
controller.Move(moveDirection * Time.deltaTime);
moveDirection.y -= gravity * Time.deltaTime;
if (Input.GetKey(KeyCode.S))
{
}
}
}
Upvotes: 0
Views: 100
Reputation: 85
You need to show Animator, how animations are playing and transitioning. Just open animator when game is playing and check if animation is playing where "Condition" parameter is passes. Check the animation transitions too. Also, I'd suggest you to change value "AnimationPar" to 0 when "s" is pressed.
if (Input.GetKey("s"))
{
anim.SetInteger ("AnimationPar", 0);
anim.SetInteger("Condition", 1);
}
Upvotes: 0