Search Unity

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

Discussion in 'Animation' started by Texasboy1306, Aug 10, 2021.

  1. Texasboy1306

    Texasboy1306

    Joined:
    Jul 26, 2021
    Posts:
    2
    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<Animator>();
    }
    // Update is called once per frame
    void Update()
    {
    Input.GetAxis("Mouse X");
    {
    animator.SetBool("IsRotating", true);
    }

    }
    }
     
    Last edited: Aug 10, 2021
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hey,

    This is what your code looks like:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class AnimationTransitionScript2 : MonoBehaviour
    5. {
    6.     Animator animator;
    7.  
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         animator = GetComponentInChildren<Animator>();
    13.     }
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         Input.GetAxis("Mouse X");
    18.         {
    19.             animator.SetBool("IsRotating", true);
    20.         }
    21.  
    22.     }
    23. }
     
    Texasboy1306 likes this.
  3. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    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:
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if (Input.GetAxis("Mouse X") != 0f)
    4.         {
    5.             animator.SetBool("IsRotating", true);
    6.         }
    7.         else
    8.         {
    9.             animator.SetBool("IsRotating", false);
    10.         }
    11.     }
    This code checks to see if "Mouse X" is not 0, and then sets "IsRotating" to true or false depending on the answer.
     
    Texasboy1306 likes this.
  4. Texasboy1306

    Texasboy1306

    Joined:
    Jul 26, 2021
    Posts:
    2
    • Unrighteouss, thank you very much, everything now works smoothly, I thank you for teaching me something new and helping me to resolve the issue.
     
    Unrighteouss likes this.