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 Help Thread

Discussion in 'Scripting' started by xDhrubovai, Sep 23, 2023.

  1. xDhrubovai

    xDhrubovai

    Joined:
    Sep 23, 2023
    Posts:
    3
    Hi,
    I am trying to make my first game but I have run into a problem. I can Run holding shift and WASD but when I release WASD after running and yet hold shift, "IsRunning" Bool doesn't get to False and thus I cannot get out of the run animation. I will be really glad if someone could help me out. Here is the code,



    Code (CSharp):
    1. [SerializeField]
    2. private bool _isMoving = false;
    3.  
    4. public bool IsMoving
    5. {
    6.      get
    7.      {
    8.          return _isMoving;
    9.      }
    10.      private set
    11.      {
    12.          _isMoving = value;
    13.          animator.SetBool(AnimationStrings.isMoving, value);
    14.      }
    15. }
    16.  
    17. [SerializeField]
    18. private bool _isRunning = false;
    19.  
    20. public bool IsRunning
    21. {
    22.      get
    23.      {
    24.          return _isRunning;
    25.      }
    26.      set
    27.      {
    28.          _isRunning = value;
    29.          animator.SetBool(AnimationStrings.isRunning, value);
    30.      }
    31. }
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    739
    well wheres the code that uses this? eg where you get your input, where are you changing IsRunning?
     
  3. xDhrubovai

    xDhrubovai

    Joined:
    Sep 23, 2023
    Posts:
    3
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using Unity.Collections.LowLevel.Unsafe;
    5. using Unity.VisualScripting;
    6. using UnityEngine;
    7. using UnityEngine.InputSystem;
    8.  
    9.  
    10. [RequireComponent(typeof(Rigidbody2D), typeof(TouchingDirections))]
    11. public class PlayerController : MonoBehaviour
    12. {
    13.     public float walkSpeed = 5f;
    14.     public float runSpeed = 8f;
    15.     public float jumpimpulse = 10f;
    16.     public float airWalkSpeed = 1f;
    17.     public int jumpCount = 0;
    18.     public int maxJumpCount = 3;
    19.     Vector2 moveInput;
    20.     TouchingDirections touchingDirections;
    21.  
    22.  
    23.     public float CurrentMoveSpeed
    24.     {
    25.         get
    26.         {
    27.             if (CanMove)
    28.             {
    29.                 if (IsMoving && !touchingDirections.IsOnWall)
    30.                 {
    31.                     if (touchingDirections.IsGrounded)
    32.                     {
    33.                         if (IsRunning)
    34.                         {
    35.                             return runSpeed;
    36.  
    37.                         }
    38.                         else
    39.                         {
    40.                             return walkSpeed;
    41.                         }
    42.                     }
    43.                     else
    44.                     {
    45.                         return airWalkSpeed;
    46.                     }
    47.                 }
    48.                 else
    49.                 {
    50.                     return 0;
    51.                 }
    52.             }
    53.             else
    54.             {
    55.                 return 0;
    56.             }
    57.  
    58.         }
    59.     }
    60.  
    61.     [SerializeField]
    62.     private bool _isMoving = false;
    63.  
    64.     public bool IsMoving
    65.     {
    66.         get
    67.         {
    68.             return _isMoving;
    69.         }
    70.         private set
    71.         {
    72.             _isMoving = value;
    73.             animator.SetBool(AnimationStrings.isMoving, value);
    74.         }
    75.     }
    76.  
    77.     [SerializeField]
    78.     private bool _isRunning = false;
    79.  
    80.     public bool IsRunning
    81.     {
    82.         get
    83.         {
    84.             return _isRunning;
    85.         }
    86.         set
    87.         {
    88.             _isRunning = value;
    89.             animator.SetBool(AnimationStrings.isRunning, value);
    90.         }
    91.     }
    92.  
    93.     public bool _isFacingRight = true;
    94.  
    95.     public bool IsFacingRight
    96.     {
    97.         get
    98.         {
    99.             return _isFacingRight;
    100.         }
    101.         private set
    102.         {
    103.             if (_isFacingRight != value)
    104.             {
    105.                 transform.localScale *= new Vector2(-1, 1);
    106.             }
    107.             _isFacingRight = value;
    108.         }
    109.     }
    110.  
    111.     Rigidbody2D rb;
    112.     Animator animator;
    113.  
    114.  
    115.     private void Awake()
    116.     {
    117.         rb = GetComponent<Rigidbody2D>();
    118.         animator = GetComponent<Animator>();
    119.         touchingDirections = GetComponent<TouchingDirections>();
    120.     }
    121.  
    122.     private void FixedUpdate()
    123.     {
    124.         rb.velocity = new Vector2(moveInput.x * CurrentMoveSpeed, rb.velocity.y);
    125.         animator.SetFloat(AnimationStrings.yVelocity, rb.velocity.y);
    126.     }
    127.     public void OnMove(InputAction.CallbackContext context)
    128.     {
    129.         moveInput = context.ReadValue<Vector2>();
    130.  
    131.         IsMoving = moveInput != Vector2.zero;
    132.  
    133.         SetFacingDirection(moveInput);
    134.  
    135.  
    136.     }
    137.  
    138.  
    139.     private void SetFacingDirection(Vector2 moveInput)
    140.     {
    141.         if (moveInput.x > 0 && !IsFacingRight)
    142.         {
    143.             IsFacingRight = true;
    144.         }
    145.         else if (moveInput.x < 0 && IsFacingRight)
    146.         {
    147.             IsFacingRight = false;
    148.         }
    149.  
    150.     }
    151.  
    152.     public bool CanMove
    153.     {
    154.         get
    155.         {
    156.             return animator.GetBool(AnimationStrings.canMove);
    157.         }
    158.     }
    159.  
    160.  
    161.     public void OnRun(InputAction.CallbackContext context)
    162.     {
    163.         if (context.started)
    164.         {
    165.             IsRunning = true;
    166.         }
    167.         else
    168.         {
    169.             IsRunning = false;
    170.         }
    171.     }
    172.  
     
  4. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    488
    Why not just check if you are moving at the end of your
    OnMove
    method, and if not then set
    IsRunning
    to false?

    Code (csharp):
    1.  
    2. public void OnMove(InputAction.CallbackContext context)
    3.     {
    4.         moveInput = context.ReadValue<Vector2>();
    5.         IsMoving = moveInput != Vector2.zero;
    6.         SetFacingDirection(moveInput);
    7.  
    8.        if (!IsMoving)
    9.        {
    10.               IsRunning = false;
    11.        }
    12.     }
     
  5. xDhrubovai

    xDhrubovai

    Joined:
    Sep 23, 2023
    Posts:
    3
    Thank you so much!
    Sometimes we forget the simplest things. It means a lot to me, thank you and take care of yourself