Search Unity

Looking For Contributor Adding to Animation script

Discussion in 'Non Commercial Collaboration' started by kingdom216, Jun 15, 2021.

  1. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    45
    I am a novice to Unity, but I have been watching a ton of tutorials and reading books about C#, and I am understanding a lot of concepts. My problem is I am needing to go further than the tutorials have gotten me. As a result of using one of the tutorials I have a 3rd person character controlled by the player, but all it will do is walk and run in place. I think this has something to do with root movement, but I am not sure.

    The second thing that has me troubled, is that I am not quite understanding how to add more key codes to the script for jumping and other animations. I tried by writing bools and setting parameters in the animator for bool is jumping. I tried writing floats and setting parameters in the animator for floats. Neither have worked for me.

    This is the code from the tutorial

    Code (Csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class animationStateController : MonoBehaviour
    8. {
    9.     Animator animator;
    10.     int isWalkingHash;
    11.     int isRunningHash;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         animator = GetComponent<Animator>();
    17.         isWalkingHash = Animator.StringToHash("isWalking");
    18.         isRunningHash = Animator.StringToHash("isRunning");
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         bool isrunning = animator.GetBool(isRunningHash);
    25.         bool isWalking = animator.GetBool(isWalkingHash);
    26.         bool forwardPressed = Input.GetKey("w");
    27.         bool runPressed = Input.GetKey("left shift");
    28.  
    29.         if (!isWalking && forwardPressed)
    30.         {
    31.             animator.SetBool(isWalkingHash, true);
    32.         }
    33.         if (isWalking && !forwardPressed)
    34.         {
    35.             animator.SetBool(isWalkingHash, false);
    36.         }
    37.  
    38.         if (!isrunning && (forwardPressed && runPressed))
    39.         {
    40.             animator.SetBool(isRunningHash, true);
    41.         }
    42.         if (isrunning && (!forwardPressed || !runPressed))
    43.         {
    44.             animator.SetBool(isRunningHash, false);
    45.         }
    46.     }
    47.    
    48. }
    49.  
    I understand what each line is saying, and I am grasping the concepts more each day, but this has me perplexed because perhaps I am not understanding it as well as I have thought. I tried adding in (keeping the isWalking and isRunning lines):

    Code (Csharp):
    1.  
    2.  
    3. int isJumpingHash;
    4.  
    5.     void Start()
    6.     {
    7.         animator = GetComponent<Animator>();
    8.         isJumpingHash = Animator.StringToHash("isJumping");
    9.  
    And then following suit with the other lines that would come after.
    Any help is appreciated.