Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Switching to and looping a new idle state

Discussion in 'Scripting' started by AngeloR2, May 14, 2021.

  1. AngeloR2

    AngeloR2

    Joined:
    Feb 24, 2021
    Posts:
    10
    I am trying to set up a script for my character that switches them from one idle state to another idle state. I did this once with my characters walk cycle to their flying idle. In this script my character walks left and right using "W.A.S.D" , flies using "Spacebar" and returns to their normal idle state using "F". See that code below:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FlightCharacterAnim : MonoBehaviour
    6. {
    7.     public Animator animator;
    8.     public float speed = 100f;
    9.     public bool grounded;
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.         if (Input.GetKeyDown(KeyCode.Space))
    15.         {
    16.             grounded = false;
    17.             animator.SetBool("Walk", false);
    18.             animator.SetBool("Flight", true);
    19.         }
    20.         if (Input.GetKeyDown(KeyCode.F))
    21.         {
    22.             grounded = true;
    23.             animator.SetBool("Flight", false);
    24.         }
    25.         if ((Input.GetAxis("Horizontal") != 0) & (grounded = true))
    26.         {
    27.             animator.SetBool("Walk", true);
    28.             transform.Translate(Vector3.right * Time.deltaTime * speed);
    29.             if (Input.GetAxis("Horizontal") < 0)
    30.             {
    31.                 transform.rotation = Quaternion.Euler(0, 180, 0);
    32.             }
    33.             if (Input.GetAxis("Horizontal") > 0)
    34.             {
    35.                 transform.rotation = Quaternion.Euler(0, 0, 0);
    36.             }
    37.         }
    38.         else
    39.         {
    40.             animator.SetBool("Walk", false);
    41.         }
    42.         if ((Input.GetAxis("Horizontal") != 0) & (grounded = false))
    43.         {
    44.             transform.Translate(Vector3.right * Time.deltaTime * speed);
    45.             if (Input.GetAxis("Horizontal") < 0)
    46.             {
    47.                 transform.rotation = Quaternion.Euler(0, 180, 0);
    48.             }
    49.             if (Input.GetAxis("Horizontal") > 0)
    50.             {
    51.                 transform.rotation = Quaternion.Euler(0, 0, 0);
    52.             }
    53.         }
    54.     }
    55. }
    I want do the same with the character going from their normal idle to their gun idle. I tried just changing the names of the animations in the script but realized that I did not know what I was doing. How would I even right a code like this out? Animator_Gun.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Traditionally, in code you set those booleans (or other properties) to values to get the animator to do what you want.

    Therefore, the changes you propose go in two places:

    - on the code side of that (what decides to set the variables)

    - on the animator side where the state diagrams and transitions go

    There's not much more to it than that.

    It's not really feasible for someone in the forums to tell you want to type, click and drag in this complicated case.

    I recommend working through one of the Unity tutorials on mecanim to refresh your brain cells.

    To debug the code side, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.

    You could also just display various important quantities in UI Text elements to watch them change as you playtest.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    To debug the Animator side, keep the window open and the object selected as you play, and you can see its state transitions.
     
  3. AngeloR2

    AngeloR2

    Joined:
    Feb 24, 2021
    Posts:
    10
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GunCharacterAnim : MonoBehaviour
    6. {
    7.     public bool gunz;
    8.     public Animator animator;
    9.  
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         if (Input.GetKeyDown(KeyCode.G))
    14.         {
    15.             gunz = true;
    16.             animator.SetBool("GunBrand", true);
    17.         }
    18.         if (Input.GetKeyDown(KeyCode.X))
    19.         {
    20.             gunz = false;
    21.             animator.SetBool("GunBrand", false);
    22.         }
    23.         if (Input.GetKeyDown(KeyCode.C))
    24.         {
    25.             animator.SetTrigger("GunFire");
    26.         }
    27.     }
    28. }
    I got it work !
     
    Kurt-Dekker likes this.