Question by
inorris · Nov 10, 2020 at 09:03 PM ·
animationanimation controller
Animation won't play
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class animationStateController : MonoBehaviour
{
Animator animator;
int isWalkingHash;
int isRunningHash;
int isWalkingBackHash;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
isWalkingHash = Animator.StringToHash("isWalking");
isRunningHash = Animator.StringToHash("isRunning");
isWalkingBackHash = Animator.StringToHash("isWalkingBack");
}
// Update is called once per frame
void Update()
{
bool isrunning = animator.GetBool(isRunningHash);
bool isWalking = animator.GetBool(isWalkingHash);
bool isWalkingBack = animator.GetBool(isWalkingBackHash);
bool forwardPressed = Input.GetKey("w");
bool runPressed = Input.GetKey("left shift");
bool backwardPressed = Input.GetKey("s");
// if player presses w key
if (!isWalking && forwardPressed)
{
// then set the isWalking boolean to be true
animator.SetBool(isWalkingHash, true);
}
// if player is not pressing w key
if (isWalking && !forwardPressed)
{
// then set the isWalking boolean to be false
animator.SetBool(isWalkingHash, false);
}
// if player is walking and not running and presses left shift
if (!isrunning && (forwardPressed && runPressed))
{
// then set the isRunning boolean to be true
animator.SetBool(isRunningHash, true);
}
// if player is running and stops running or stops walking
if (isrunning && (!forwardPressed || !runPressed))
{
// then set the isRunning boolean to be false
animator.SetBool(isRunningHash, false);
}
// if player presses s key
if (!isWalkingBack && backwardPressed)
{
//then set the isWalkingBack boolean to be true
animator.SetBool(isWalkingBackHash, true);
}
// if player is not pressing s key
if (isWalkingBack && !backwardPressed)
{
// then set the isWalkingBack boolean to be false
animator.SetBool(isWalkingBackHash, false);
}
}
}
My Walk forward, idle, and Sprint animations work but for some reason, my walking backward animation won't work! Why is this happening?
Comment