Animation Input Not Working!!!!!
So i'm working on a story game, and I'm making animations. I have made a functionality for these Animations:
Move Forward
Idle
Shoot While Moving Forward
And also crouch while moving forward
For some reason I need to hold control first then w to start the Crouch while moving forward animation. Also when I release Control the animation is still playing, the other are working well, this is the Code:
using UnityEngine;
namespace Tobi.Animaton{
public class Animation : MonoBehaviour
{
Animator animator;
void Start()
{
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
bool pressingControl = Input.GetKey(KeyCode.LeftControl);
bool holdingClick = Input.GetMouseButton(0);
if (Input.GetKey("w"))
{
if(pressingControl){
Debug.Log("Crouching");
animator.SetBool("Walk", false);
animator.SetBool("Shoot For", false);
animator.SetBool("Crawl For", true);
animator.SetBool("Idle", false);
}
else if(holdingClick) {
Debug.Log("Shooting");
animator.SetBool("Walk", false);
Debug.Log("Pressed left click.");
animator.SetBool("Shoot For", true);
animator.SetBool("Crawl For", false);
animator.SetBool("Idle", false);
}else if (!holdingClick && !pressingControl)
{
animator.SetBool("Walk", true);
animator.SetBool("Shoot For", false);
animator.SetBool("Crawl For", false);
animator.SetBool("Idle", false);
}
}else{
Debug.Log("Idling");
animator.SetBool("Walk", false);
animator.SetBool("Shoot For", false);
animator.SetBool("Crawl For", false);
animator.SetBool("Idle", true);
}
}
}
}
Hello NeoBossGaming,
Your code looks alright, and I suspect the problem is in the way the transitions are setup in the animator. It would be useful if you could update your question with more information on the animator setup.
As an aside, I believe your code can be improved for readability (this is up to you though):
void Update()
{
bool pressingControl = Input.GetKey(KeyCode.LeftControl);
bool holdingClick = Input.GetMouseButton(0);
bool forward = Input.GetKey("w");
animator.SetBool("Walk", forward && !pressingControl && !holdingClick);
animator.SetBool("Shoot For", forward && !pressingControl && holdingClick);
animator.SetBool("Crawl For", forward && pressingControl);
animator.SetBool("Idle", !forward);
}
Your answer
Follow this Question
Related Questions
I cannot drag the animations into inspector please help [code snippet included] [C#] 0 Answers
How can I animate my 3D object without affecting the physics? 0 Answers
how to make an animation to play when a int has a certain value, and pressing space key? 0 Answers
Unity and Android input touch? 1 Answer