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

Bug Animation not playing

Discussion in 'Scripting' started by TrexMatrix, Sep 4, 2023.

  1. TrexMatrix

    TrexMatrix

    Joined:
    Aug 8, 2023
    Posts:
    39
    I am making a soulslike game and watching this video currently:
    i got to 5:30 but when I try to roll the play sinks into the ground and it says this.

    Parameter 'IsInteracting' does not exist.
    UnityEngine.Animator:SetBool (string,bool)
    HR.AnimatorHandler:playTargetAnimation (string,bool) (at Assets/Scripts/AnimatorHandler.cs:80)
    HR.PlayerLocomotion:HandleRollingAndSprinting (single) (at Assets/Scripts/PlayerLocomotion.cs:114)
    HR.PlayerLocomotion:Update () (at Assets/Scripts/PlayerLocomotion.cs:44)
     
  2. TrexMatrix

    TrexMatrix

    Joined:
    Aug 8, 2023
    Posts:
    39
    Also here are my 2 scripts:
    Code (SCharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace HR
    6. {
    7.     public class AnimatorHandler : MonoBehaviour
    8.     {
    9.         public Animator anim;
    10.         int vertical;
    11.         int horizontal;
    12.         public bool canRotate;
    13.  
    14.         public void Initialize()
    15.         {
    16.             anim = GetComponent<Animator>();
    17.             vertical = Animator.StringToHash("Vertical");
    18.             horizontal = Animator.StringToHash("Horizontal");
    19.         }
    20.  
    21.         public void UpdateAnimatorValues(float verticalMovement, float horizontalMovement)
    22.         {
    23.             #region Vertical
    24.             float v = 0;
    25.  
    26.             if (verticalMovement > 0 && verticalMovement < 0.55f)
    27.             {
    28.                 v = 0.5f;
    29.             }
    30.             else if (verticalMovement > 0.55f)
    31.             {
    32.                 v = 1;
    33.             }
    34.             else if (verticalMovement < 0 && verticalMovement > -0.55f)
    35.             {
    36.                 v = -0.5f;
    37.             }
    38.             else if (verticalMovement < -0.55f)
    39.             {
    40.                 v = -1;
    41.             }
    42.             else
    43.             {
    44.                 v = 0;
    45.             }
    46.             #endregion
    47.  
    48.             #region Horizontal;
    49.             float h = 0;
    50.  
    51.             if (horizontalMovement > 0 && horizontalMovement < 0.55f)
    52.             {
    53.                 h = 0.5f;
    54.             }
    55.             else if (horizontalMovement > 0.55f)
    56.             {
    57.                 h = 1;
    58.             }
    59.             else if (horizontalMovement < 0 && horizontalMovement > -0.55f)
    60.             {
    61.                 h = -0.5f;
    62.             }
    63.             else if (horizontalMovement < -0.55f)
    64.             {
    65.                 h = -1;
    66.             }
    67.             else
    68.             {
    69.                 h = 0;
    70.             }
    71.             #endregion
    72.  
    73.             anim.SetFloat(vertical, v, 0.1f, Time.deltaTime);
    74.             anim.SetFloat(horizontal, h, 0.1f, Time.deltaTime);
    75.         }
    76.  
    77.         public void PlayTargetAnimation(string targetAnim, bool isInteracting)
    78.         {
    79.             anim.applyRootMotion = isInteracting;
    80.             anim.SetBool("IsInteracting", isInteracting);
    81.             anim.CrossFade(targetAnim, 0.2f);
    82.         }
    83.  
    84.         public void CanRotate()
    85.         {
    86.             canRotate = true;
    87.         }
    88.  
    89.         public void StopRotation()
    90.         {
    91.             canRotate = false;
    92.         }
    93.     }
    94. }
    95.  
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. namespace HR
    7. {
    8.     public class PlayerLocomotion : MonoBehaviour
    9.     {
    10.         Transform cameraObject;
    11.         InputHandler inputHandler;
    12.         Vector3 moveDirection;
    13.  
    14.         [HideInInspector]
    15.         public Transform myTransform;
    16.         [HideInInspector]
    17.         public AnimatorHandler animatorHandler;
    18.  
    19.         public new Rigidbody rigidbody;
    20.         public GameObject normalCamera;
    21.  
    22.         [Header("Stats")]
    23.         [SerializeField]
    24.         float movementSpeed = 5;
    25.         [SerializeField]
    26.         float rotationSpeed = 10;
    27.  
    28.         void Start()
    29.         {
    30.             rigidbody = GetComponent<Rigidbody>();
    31.             inputHandler = GetComponent<InputHandler>();
    32.             animatorHandler = GetComponentInChildren<AnimatorHandler>();
    33.             cameraObject = Camera.main.transform;
    34.             myTransform = transform;
    35.             animatorHandler.Initialize();
    36.  
    37.         }
    38.  
    39.         public void Update()
    40.         {
    41.             float delta = Time.deltaTime;
    42.  
    43.             inputHandler.TickInput(delta);
    44.             HandleMovement(delta);
    45.             HandleRollingAndSprinting(delta);
    46.  
    47.         }
    48.  
    49.         #region Movement
    50.         Vector3 normalVector;
    51.         Vector3 targetPosition;
    52.  
    53.         private void HandleRotation(float delta)
    54.         {
    55.             Vector3 targetDir = Vector3.zero;
    56.             float moveOverride = inputHandler.moveAmount;
    57.  
    58.             targetDir = cameraObject.forward * inputHandler.vertical;
    59.             targetDir += cameraObject.right * inputHandler.horizontal;
    60.  
    61.             targetDir.Normalize();
    62.             targetDir.y = 0;
    63.  
    64.             if (targetDir == Vector3.zero)
    65.             targetDir = myTransform.forward;
    66.  
    67.             float rs = rotationSpeed;
    68.  
    69.             Quaternion tr = Quaternion.LookRotation(targetDir);
    70.             Quaternion targetRotation = Quaternion.Slerp(myTransform.rotation,tr, rs * delta);
    71.  
    72.             myTransform.rotation = targetRotation;
    73.         }
    74.  
    75.         public void HandleMovement(float delta)
    76.         {
    77.             moveDirection = cameraObject.forward * inputHandler.vertical;
    78.             moveDirection += cameraObject.right * inputHandler.horizontal;
    79.             moveDirection.Normalize();
    80.             moveDirection.y = 0;
    81.  
    82.             float speed = movementSpeed;
    83.             moveDirection *= speed;
    84.  
    85.             Vector3 projectedVelocity = Vector3.ProjectOnPlane(moveDirection, normalVector);
    86.             rigidbody.velocity = projectedVelocity;
    87.  
    88.             animatorHandler.UpdateAnimatorValues(inputHandler.moveAmount, 0);
    89.  
    90.             if (animatorHandler.canRotate)
    91.             {
    92.                 HandleRotation(delta);
    93.             }
    94.         }
    95.  
    96.         public void HandleRollingAndSprinting(float delta)
    97.         {
    98.             if (animatorHandler.anim.GetBool("isInteracting"))
    99.                 return;
    100.  
    101.             if (inputHandler.rollFlag)
    102.             {
    103.                 moveDirection = cameraObject.forward * inputHandler.vertical;
    104.                 moveDirection += cameraObject.right * inputHandler.horizontal;
    105.  
    106.                 if (inputHandler.moveAmount > 0)
    107.                 {
    108.                     animatorHandler.PlayTargetAnimation("Rolling", true);
    109.                     moveDirection.y = 0;
    110.                     Quaternion rollRotation = Quaternion.LookRotation(moveDirection);
    111.                     myTransform.rotation = rollRotation;
    112.                 }
    113.                 else
    114.                 {
    115.                     animatorHandler.PlayTargetAnimation("Backstep", true);
    116.                 }
    117.             }
    118.         }
    119.  
    120.         #endregion
    121.     }
    122. }
    123.  
     
  3. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    488
    The error is telling you exactly what is wrong. If you look at your animator, you probably did not name the parameter
    IsInteracting
    . Especially if you look at the video around the 1 minute, the parameter is clearly named
    isInteracting
    . Notice the lower-case i?

    Also at around 2:16 in the video he clearly types
    isInteracting
    in his script for the as the animation parameter to be updated.

    So change your script to use the exact name of the parameter.
     
    wideeyenow_unity likes this.