How do I get an Animation playing when my character rotates on the spot?

Hey guys, I’m making a First Person Game and I’m wanting to have an animation that plays when the character rotates on the spot, I’ve written a prototype script, but it doesn’t seem to work, well it kind of does, just the animation plays as soon as the game starts, Does anyone know how to have an animation play when the character rotates on the spot? here is the code that I had written and in the Animator component I’m using a bool paramatter. ( When I mention character rotates on the spot, I’m mentioning how when the player starts looking around with his mouse or mousepad when standing still/Idle.)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationTransitionScript2 : MonoBehaviour
{
Animator animator;

// Start is called before the first frame update
void Start()
{
animator = GetComponentInChildren();
}
// Update is called once per frame
void Update()
{
Input.GetAxis(“Mouse X”);
{
animator.SetBool(“IsRotating”, true);
}

}
}

Hey,

This is what your code looks like:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationTransitionScript2 : MonoBehaviour
{
    Animator animator;


    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponentInChildren<Animator>();
    }
    // Update is called once per frame
    void Update()
    {
        Input.GetAxis("Mouse X");
        {
            animator.SetBool("IsRotating", true);
        }

    }
}
1 Like

The issue is that you aren’t actually doing anything on line 17.

You have some curly brackets on lines 18 and 20, so it looks like you’re trying to create an if statement, but you have a semi-colon on line 17, so the following lines are unrelated.

I think what you’re looking for is this:

    void Update()
    {
        if (Input.GetAxis("Mouse X") != 0f)
        {
            animator.SetBool("IsRotating", true);
        }
        else
        {
            animator.SetBool("IsRotating", false);
        }
    }

This code checks to see if “Mouse X” is not 0, and then sets “IsRotating” to true or false depending on the answer.

1 Like
  • Unrighteouss, thank you very much, everything now works smoothly, I thank you for teaching me something new and helping me to resolve the issue.
1 Like