Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

What is wrong with my code?

Discussion in 'Animation' started by cl770126, Nov 30, 2021.

  1. cl770126

    cl770126

    Joined:
    Sep 15, 2021
    Posts:
    1
    I'm new to unity and I'm trying to get my character to do a idle, walk, run blend tree. Both the idle and walk work perfectly fine with my code but the run just doesn't seem to work, does anyone know what's wrong and possibly give a fixed version of the code?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ThirdPersonMovement : MonoBehaviour
    6. {
    7.     public CharacterController controller;
    8.     public Transform cam;
    9.  
    10.     public float walkSpeed = 3f;
    11.     public float runSpeed = 6f;
    12.     public float gravity = -9.81f;
    13.  
    14.     public bool isSprinting = false;
    15.     public Animator anim;
    16.  
    17.     public float turnSmoothTime = 0.1f;
    18.     float turnSmoothVelocity;
    19.  
    20.     Vector3 velocity;
    21.  
    22.     // Update is called once per frame
    23.     void Start()
    24.     {
    25.         anim = GetComponentInChildren<Animator>();
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         float horizontal = Input.GetAxisRaw("Horizontal");
    31.         float vertical = Input.GetAxisRaw("Vertical");
    32.         Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
    33.  
    34.         if (Input.GetKey(KeyCode.LeftShift))
    35.         {
    36.             isSprinting = true;
    37.  
    38.             if (direction.magnitude >= 6.0f)
    39.             {
    40.                 float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    41.                 float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    42.                 transform.rotation = Quaternion.Euler(0f, angle, 0f);
    43.  
    44.                 Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    45.                 controller.Move(moveDir * runSpeed * Time.deltaTime);
    46.  
    47.                 velocity.y += gravity * Time.deltaTime;
    48.                 controller.Move(velocity * Time.deltaTime);
    49.             }
    50.             else
    51.             {
    52.                 isSprinting = false;
    53.  
    54.                 if (direction.magnitude >= 0.1f)
    55.                 {
    56.                     float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    57.                     float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    58.                     transform.rotation = Quaternion.Euler(0f, angle, 0f);
    59.  
    60.                     Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    61.                     controller.Move(moveDir * walkSpeed * Time.deltaTime);
    62.  
    63.                     velocity.y += gravity * Time.deltaTime;
    64.                     controller.Move(velocity * Time.deltaTime);
    65.                     anim.SetFloat("Speed", .5f);
    66.                 }
    67.             }
    68.         }
    69.         if(direction.magnitude >= 0.1f)
    70.         {
    71.             float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    72.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    73.             transform.rotation = Quaternion.Euler(0f, angle, 0f);
    74.  
    75.             Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    76.             controller.Move(moveDir * walkSpeed * Time.deltaTime);
    77.  
    78.             velocity.y += gravity * Time.deltaTime;
    79.             controller.Move(velocity * Time.deltaTime);
    80.             anim.SetFloat("Speed", .5f);
    81.         }
    82.         else
    83.         {
    84.             anim.SetFloat("Speed", 0);
    85.         }
    86.     }
    87.     private void Idle()
    88.     {
    89.  
    90.  
    91.     }
    92.     private void Walk()
    93.     {
    94.  
    95.     }
    96.     private void Run()
    97.     {
    98.      if (isSprinting == true)
    99.      {
    100.         anim.SetFloat("Speed", 1f);
    101.      }
    102.     }
    103. }
    104.