Search Unity

Bug Player move backward

Discussion in 'Input System' started by Henrique_Fieb, Mar 9, 2023.

?

player bug moving to backwards in new input system

  1. rigidbody error

    0 vote(s)
    0.0%
  2. Velocity code bug

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. Henrique_Fieb

    Henrique_Fieb

    Joined:
    Aug 16, 2022
    Posts:
    1
    i am a brasilian studant to game development (my english is very bad sorry).

    I am making a 2d plataform game to test unity codes. Initially the project use original input maneger system but i wanted test de new input system.
    After implementation new input system the player character is moving backward when i jump and colide in a wall

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Animations;
    5. using UnityEngine.InputSystem;
    6. using UnityEngine.InputSystem.Haptics;
    7. using UnityEngine.UI;
    8.  
    9. public class Player : MonoBehaviour
    10. {
    11.     private bool _pular = false; // Variavel que recebe se um valor de pulo foi acionado
    12.  
    13.     [SerializeField]
    14.     public float _speed = 5f; // velocidade de movimento
    15.  
    16.     [SerializeField]
    17.     private float _jump; // Força do pulo
    18.  
    19.     private Rigidbody2D rig; // Componente Rigdbody2D
    20.  
    21.     private bool isJumping;
    22.  
    23.     private bool doublejump;
    24.  
    25.     private Animator anim; // Componente Animator
    26.  
    27.     private float auxJump;
    28.  
    29.     private float auxMove;
    30.  
    31.     private float auxMoveSpeed;
    32.     private float auxJumpSpeed;
    33.  
    34.     [SerializeField] private SelecaoSkin skin;
    35.  
    36.  
    37.     [SerializeField] private Sprite sprite;
    38.  
    39.  
    40.     private void Start()
    41.     {
    42.         rig = GetComponent<Rigidbody2D>(); // Importando o componente Rigidbody 2d para o script
    43.  
    44.         anim = GetComponent<Animator>(); // Importando o componente Animator para o script
    45.  
    46.         changeSkin();
    47.     }
    48.  
    49.  
    50.  
    51.     private void Update()
    52.     {
    53.         // move(); // Movimento
    54.         //jump(); // Pulo
    55.  
    56.  
    57.  
    58.  
    59.         if (transform.position.y <= -36)
    60.         {
    61.  
    62.             transform.position = new Vector2(-17.56f, -22.0f);
    63.  
    64.         }
    65.  
    66.  
    67.     }
    68.  
    69.  
    70.  
    71.    
    72.     public void move(InputAction.CallbackContext value) {
    73.        
    74.         auxMove = value.ReadValue<float>();
    75.  
    76.         /*if (auxMove != 0)
    77.         {
    78.             Mobilemove();
    79.         } else
    80.         {
    81.         rig.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * _speed, rig.velocity.y);
    82.         }*/
    83.  
    84.        
    85.         rig.velocity = new Vector2(auxMove * _speed, rig.velocity.y);
    86.  
    87.      
    88.  
    89.  
    90.         if (auxMove > 0)
    91.         {
    92.             auxMove = 1;
    93.             transform.eulerAngles = new Vector3(0, 0, 0);
    94.             anim.SetBool("walk", true);
    95.  
    96.  
    97.         }
    98.         else if (auxMove < 0)
    99.         {
    100.             auxMove = -1;
    101.  
    102.             transform.eulerAngles = new Vector3(0, 180, 0);
    103.             anim.SetBool("walk", true);
    104.  
    105.         }
    106.         else if (auxMove == 0)
    107.         {
    108.             anim.SetBool("walk", false);
    109.         }
    110.  
    111.        
    112.  
    113.     }
    114.  
    115.  
    116.     public void jump(InputAction.CallbackContext value) {
    117.  
    118.         _pular = value.performed;
    119.  
    120.         if(_pular)
    121.         {
    122.             if (!isJumping)
    123.             {
    124.                 Gamepad.current.SetMotorSpeeds(0.25f, 0.75f);
    125.                 isJumping = true;
    126.                 //auxJump--;
    127.                 anim.SetBool("jump", true);
    128.                 rig.AddForce(new Vector2(0f, _jump), ForceMode2D.Impulse);
    129.                 doublejump = true;
    130.                 _pular = false;
    131.             }
    132.             else if (doublejump)
    133.             {
    134.                 Gamepad.current.SetMotorSpeeds(0.25f, 0.75f);
    135.  
    136.                 // auxJump--;
    137.  
    138.                 doublejump = false;
    139.  
    140.                 rig.AddForce(new Vector2(0f, _jump), ForceMode2D.Impulse);
    141.                 _pular = false;
    142.  
    143.             }
    144.         }
    145.  
    146.     }
    147.  
    148.         private void OnCollisionEnter2D(Collision2D other)
    149.         {
    150.             if (other.gameObject.layer == 6)
    151.             {
    152.                 GameControler.instance.GameOver();
    153.                 Destroy(this.gameObject);
    154.             }
    155.  
    156.             if (other.gameObject.layer == 3)
    157.             {
    158.                 isJumping = false;
    159.                 anim.SetBool("jump", false);
    160.  
    161.             }
    162.         }
    163.  
    164.  
    165.  
    166.         private void OnitCollisionExit2D(Collision2D other)
    167.         {
    168.             if (other.gameObject.layer == 3) // se o objeto colidido estiver na camada 3(chao)
    169.             {
    170.                 isJumping = true; // Definindo isJumping como verdadeiro
    171.             }
    172.         }
    173.  
    174.  
    175.        
    176.        
    177.  
    178.  
    179.      
    180.         public void changeSkin() // Sistema de troca de skin
    181.         {
    182.             if (GameControler.playerSkin != null)
    183.             {
    184.                 skin = GameControler.playerSkin;
    185.             }
    186.  
    187.             anim.runtimeAnimatorController = skin.animControler;
    188.  
    189.             sprite = skin.sprite;
    190.  
    191.  
    192.  
    193.  
    194.  
    195.  
    196.         }
    197. }
    198.