Search Unity

Can't find what I am doing wrong

Discussion in 'Scripting' started by kingdom216, Jun 30, 2021.

  1. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    38
    I have followed this tutorial:

    and once again I have gotten to a part that I am stuck at. Upon pressing the walk key, the player lifts in the air and runs on the axii, but above the plane. Also the player skips over the walk command right to the run and then doesn't stop the running animation until exiting play mode. I feel that I must add that the tutorial has a place in the script that multiplies by Time.deltaTime, but when I applied this the player remained in place when the animations were active. Any help here would be great.

    Code (Csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6.  
    7. public class AnimationMotionController : MonoBehaviour
    8. {
    9.     PlayerInput playerInput;
    10.     CharacterController characterController;
    11.     Animator animator;
    12.  
    13.     int isWalkingHash;
    14.     int isRunningHash;
    15.  
    16.     Vector2 currentMovementInput;
    17.     Vector3 currentMovement;
    18.     Vector3 currentRunMovement;
    19.     bool isMovementPressed;
    20.     bool isRunPressed;
    21.     float rotationFactorPerFrame = 1.0f;
    22.     float runMultiplier = 3.0f;
    23.  
    24.  
    25.     void Awake()
    26.     {
    27.         playerInput = new PlayerInput();
    28.         characterController = GetComponent<CharacterController>();
    29.         animator = GetComponent<Animator>();
    30.  
    31.         isWalkingHash = Animator.StringToHash("isWalking");
    32.         isRunningHash = Animator.StringToHash("isRunning");
    33.  
    34.         playerInput.CharacterControls.Move.started += onMovementInput;
    35.         playerInput.CharacterControls.Move.canceled += onMovementInput;
    36.         playerInput.CharacterControls.Move.performed += onMovementInput;
    37.         playerInput.CharacterControls.Run.started += onRun;
    38.         playerInput.CharacterControls.Run.canceled += onRun;
    39.     }
    40.  
    41.     void onRun(InputAction.CallbackContext context)
    42.     {
    43.         isRunPressed = context.ReadValueAsButton();
    44.     }
    45.  
    46.     void handleRotation()
    47.     {
    48.         Vector3 positionToLookAt;
    49.  
    50.         positionToLookAt.x = currentMovement.x;
    51.         positionToLookAt.y = 0.0f;
    52.         positionToLookAt.z = currentMovement.z;
    53.         Quaternion currentRotation = transform.rotation;
    54.  
    55.        
    56.  
    57.         if (isMovementPressed) {
    58.             Quaternion targetRotation = Quaternion.LookRotation(positionToLookAt);
    59.             transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame);
    60.         }
    61.     }
    62.  
    63.     void onMovementInput(InputAction.CallbackContext context)
    64.     {
    65.         currentMovementInput = context.ReadValue<Vector2>();
    66.         currentMovement.x = currentMovementInput.x;
    67.         currentMovement.z = currentMovementInput.y;
    68.         currentRunMovement.x = currentMovementInput.x * runMultiplier;
    69.         currentRunMovement.z = currentMovementInput.y * runMultiplier;
    70.         isMovementPressed = currentMovementInput.x != 0 || currentMovementInput.y != 0;
    71.     }
    72.  
    73.     void handleAnimation()
    74.     {
    75.         bool isWalking = animator.GetBool(isWalkingHash);
    76.         bool isRunning = animator.GetBool(isRunningHash);
    77.  
    78.         if(isMovementPressed && !isWalking) {
    79.             animator.SetBool(isWalkingHash, true);
    80.         }
    81.  
    82.         else if (!isMovementPressed && isWalking) {
    83.             animator.SetBool(isWalkingHash, false);
    84.         }
    85.  
    86.         if ((isMovementPressed && isRunPressed) && !isRunning)
    87.         {
    88.             animator.SetBool(isRunningHash, true);
    89.         }
    90.  
    91.         else if ((!isMovementPressed || !isRunPressed) && isRunning) {
    92.             animator.SetBool(isRunningHash, false);
    93.         }
    94.     }
    95.  
    96.     void HandleGravity()
    97.     {
    98.         if(characterController.isGrounded)
    99.         {
    100.             float groundedGravity = -.05f;
    101.             currentMovement.y = groundedGravity;
    102.             currentRunMovement.y = groundedGravity;
    103.         } else {
    104.             float gravity = -9.8f;
    105.             currentMovement.y = gravity;
    106.             currentRunMovement.y = gravity;
    107.         }
    108.     }
    109.  
    110.     // Update is called once per frame
    111.     void Update()
    112.     {
    113.         handleRotation();
    114.         handleAnimation();
    115.  
    116.         if (isRunPressed) {
    117.             characterController.Move(currentRunMovement);
    118.         } else {
    119.              characterController.Move(currentMovement);
    120.         }
    121.  
    122.        
    123.     }
    124.  
    125.     void OnEnable()
    126.     {
    127.         playerInput.CharacterControls.Enable();
    128.     }
    129.  
    130.     void OnDisable()
    131.     {
    132.         playerInput.CharacterControls.Disable();
    133.     }
    134. }
    135.  
     
    Last edited: Jul 11, 2021
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    2,858
    Working on hunches here since I'm not going to be watching a 27 minute long video.

    You've probably have "Root Motion" on in your animator, which is overriding and driving all of your transform values and why you're not seeing any real movement. You definitely want to multiply your movement by delta time. That could also explain why your character is moving in an odd direction, as your code is quite explicit about moving only on x/z.

    As for the running issue, it's probably something in your animation states or transitions. Re-watch that part of the video and make sure you have it exactly the same, because you're not triggering any running in the code you've presented here, so it must be there.
     
  3. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    38
    Figured it all out, simple mistakes. There was a part of the tutorial where he went through the code really fast and I ended up missing a step. As for raising on the Y axis before animation: Capture671.png
    I changed the Y from 0 to 2.5 Now the player remains on the plane while doing the animations. Thanks for the help, it set me on a new path of thinking.
     
    GroZZleR likes this.
unityunity