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 new Input System bug/code error

Discussion in 'Input System' started by unity_9C74F6DA7C9659EF8929, May 22, 2023.

  1. unity_9C74F6DA7C9659EF8929

    unity_9C74F6DA7C9659EF8929

    Joined:
    May 18, 2023
    Posts:
    1
    I found myself with a bug when I try to Jump while my character is moving towards W+A and while running in any direction involving W direction My player wont jump and after debugging for a day in a state where im questioning my sanity I downloaded the 2021 Unity version with the third person core project I found it and noticed that has the exaclty same bugs that i've found, so with this I question if it's a bug wish the current system at least I think it's with the new input system but could be some kinda of programming error , or a logic bug idk, but I will drop my code if anybody can find something I would be eternally greatful because I really lost my sanity

    Note: tested with both RigidBody and CharacterController
    Note: tried with both performed and started states.

    Code with Rgid Body

    Code (CSharp):
    1.  
    2.  
    3.     [Header("Config Player")]
    4.  
    5.     [SerializeField] private float speed = 3f;
    6.     [SerializeField] private float speedWhileRunningMultiplier = 1.3f;
    7.     [SerializeField] private float gravity = -9.8f;
    8.     [SerializeField] private float gravityMultiplier = 3f;
    9.     [SerializeField] private float jumpForce = 9.8f;
    10.     [SerializeField] private float smoothTime = 0.05f;
    11.  
    12.     private Rigidbody rb;
    13.     private Animator anim;
    14.  
    15.     private bool isWalking, isRunning = false;//for animation purposes
    16.  
    17.  
    18.     private float currentSpeedRotationing;
    19.     private float verticalVelocity = 0f;
    20.  
    21.     private Vector2 movement2D;
    22.     private Vector3 direction;
    23.  
    24.     private void Awake()
    25.     {
    26.         rb = GetComponent<Rigidbody>();
    27.  
    28.         anim = GetComponent<Animator>();
    29.     }
    30.  
    31.     void Update()
    32.     {
    33.         ApplyMovement();
    34.  
    35.     }
    36.  
    37.     public void SetMovement(InputAction.CallbackContext value)//usa o parametro pra recer resultado das action (nessa caso vetor2
    38.     {
    39.         movement2D = value.ReadValue<Vector2>();
    40.     }
    41.  
    42.     public void SetJump(InputAction.CallbackContext value)//usa o parametro pra recer resultado das action (nessa caso vetor2
    43.     {
    44.         if (value.performed)
    45.         {//perfomed é se eu ainda estou segurando o botão(espaço) started quando eu cliquei no botão canceled quando eu soltei o botão
    46.  
    47.         rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
    48.         }
    49.     }
    50.     public void SetRun(InputAction.CallbackContext value)
    51.     {
    52.         isRunning = value.performed ? true : false;
    53.     }
    54.  
    55.     void ApplyMovement()
    56.     {
    57.         direction = new Vector3(movement2D.x, 0, movement2D.y);
    58.  
    59.         isWalking = direction.x != 0 || direction.z != 0;
    60.  
    61.  
    62.         float currentSpeed = speed * (isRunning ? speedWhileRunningMultiplier : 1f);
    63.         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), 0.15f);
    64.         transform.Translate(direction * currentSpeed * Time.deltaTime, Space.World);
    65.         anim.SetBool("isWalking", isWalking);
    66.         anim.SetBool("isRunning", isRunning);
    67.     }
    68.  
    69.  
    70.  
    Code with Character Controller
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     [Header("Config Player")]
    8.  
    9.     [SerializeField] private float speed = 3f;
    10.     [SerializeField] private float speedWhileRunningMultiplier = 1.3f;
    11.     [SerializeField] private float gravity = -9.8f;
    12.     [SerializeField] private float gravityMultiplier = 3f;
    13.     [SerializeField] private float jumpForce = 9.8f;
    14.     [SerializeField] private float smoothTime = 0.05f;
    15.  
    16.     private CharacterController controller;
    17.     private Animator anim;
    18.  
    19.     private bool isWalking, isRunning = false;//for animation purposes
    20.  
    21.  
    22.     private float currentSpeedRotationing;
    23.     private float verticalVelocity = 0f;
    24.  
    25.     private Vector2 movement2D;
    26.     private Vector3 direction;
    27.  
    28.     private bool IsGrounded() => controller.isGrounded;
    29.     private void Awake()
    30.     {
    31.         controller = GetComponent<CharacterController>();
    32.         anim = GetComponent<Animator>();
    33.     }
    34.  
    35.     void FixedUpdate()
    36.     {
    37.         ApplyGravity();
    38.         ApplyMovement();
    39.         ApplyRotation();
    40.     }
    41.  
    42.     public void SetMovement(InputAction.CallbackContext value)//usa o parametro pra recer resultado das action (nessa caso vetor2
    43.     {
    44.         movement2D = value.ReadValue<Vector2>();
    45.         movement2D.Normalize();
    46.         direction = new Vector3(movement2D.x, 0, movement2D.y);
    47.         direction.Normalize();
    48.     }
    49.  
    50.     public void SetJump(InputAction.CallbackContext value)//usa o parametro pra recer resultado das action (nessa caso vetor2
    51.     {
    52.         if (value.performed) Debug.Log("ehne");
    53.         if (!value.started) return;//perfomed é se eu ainda estou segurando o botão(espaço) started quando eu cliquei no botão canceled quando eu soltei o botão
    54.         if (!IsGrounded()) return;
    55.  
    56.         verticalVelocity += jumpForce;
    57.     }
    58.     public void SetRun(InputAction.CallbackContext value)
    59.     {
    60.         isRunning = value.performed ? true : false;
    61.     }
    62.  
    63.     void ApplyMovement()
    64.     {
    65.  
    66.         isWalking = direction.x != 0 || direction.z != 0;
    67.  
    68.  
    69.         float currentSpeed = speed * (isRunning ? speedWhileRunningMultiplier : 1f);
    70.  
    71.         controller.Move(direction * currentSpeed * Time.deltaTime);
    72.         anim.SetBool("isWalking", isWalking);
    73.         anim.SetBool("isRunning", isRunning);
    74.     }
    75.     void ApplyRotation()
    76.     {
    77.         if (movement2D.sqrMagnitude == 0) return;
    78.  
    79.         float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
    80.         float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref currentSpeedRotationing, smoothTime);
    81.         transform.rotation = Quaternion.Euler(0.0f, angle, 0.0f);
    82.  
    83.     }
    84.     void ApplyGravity()
    85.     {
    86.         if (IsGrounded() && verticalVelocity <0.0f)
    87.         {
    88.             verticalVelocity = -1f;
    89.         }
    90.         else
    91.         {
    92.         verticalVelocity += gravity * gravityMultiplier * Time.deltaTime;
    93.         }
    94.         direction.y =  verticalVelocity;
    95.     }
    96. }
    97.  
    98.  
     
    Last edited: May 22, 2023