Search Unity

Resolved Cant jump while moving left or right

Discussion in 'Getting Started' started by DR_Cheese1, Aug 25, 2023.

  1. DR_Cheese1

    DR_Cheese1

    Joined:
    Aug 25, 2023
    Posts:
    6
    I cant move while jumping left or right and i dont know how to fix it

    Here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.VisualScripting;
    4. using UnityEngine;
    5.  
    6. public class Playermovement : MonoBehaviour
    7. {
    8.     [SerializeField] GameObject Deathcontroll;
    9.     [SerializeField] Rigidbody2D rb;
    10.     [SerializeField] float jumppower;
    11.     [SerializeField] bool Canjump;
    12.     [SerializeField] float movepower;
    13.     [SerializeField] bool alive;
    14.     [SerializeField] bool Dpress;
    15.     [SerializeField] bool Apress;
    16.     private logiccontroller controller;
    17.     private bool spacedown;
    18.  
    19.     // Start is called before the first frame update
    20.  
    21.     private void Awake()
    22.     {
    23.        
    24.     }
    25.  
    26.     void Start()
    27.     {
    28.         rb = GetComponent<Rigidbody2D>();
    29.         alive = true;
    30.         controller = Deathcontroll.GetComponent<logiccontroller>();
    31.     }
    32.    
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.         if (controller.isdead == true)
    37.         {
    38.             alive = false;
    39.         }
    40.         else
    41.         {
    42.             alive = true;
    43.         }
    44.  
    45.  
    46.         if (Input.GetKey(KeyCode.D))
    47.         {
    48.             Dpress = true;  
    49.         }
    50.         else
    51.         {
    52.             Dpress = false;
    53.         }
    54.  
    55.         if (Input.GetKey(KeyCode.A))
    56.         {
    57.             Apress = true;
    58.         }
    59.         else
    60.         {
    61.             Apress = false;
    62.         }
    63.  
    64.         if (Input.GetKeyDown(KeyCode.Space))
    65.         {
    66.             spacedown = true;
    67.         }
    68.         else
    69.         {
    70.             spacedown = false;
    71.         }
    72.  
    73.         if (Canjump == true && spacedown == true && alive == true)
    74.         {
    75.             rb.velocity = Vector2.up * jumppower;
    76.             controller.jumped = true;
    77.         }
    78.         else
    79.         {
    80.             controller.jumped = false;
    81.         }
    82.  
    83.         if (Dpress == true && alive == true)
    84.         {
    85.             rb.velocity = Vector2.right * movepower;
    86.         }
    87.  
    88.         if (Apress == true && alive == true)
    89.         {
    90.             rb.velocity = Vector2.left * movepower;
    91.         }
    92.  
    93.         if (controller.reset == true)
    94.         {
    95.             gameObject.transform.position = new Vector3(0 , 0, 0);
    96.         }
    97.     }
    98.  
    99.     void OnCollisionEnter2D(Collision2D collision)
    100.     {
    101.         if (collision.gameObject.tag == "grass")
    102.         {
    103.             Canjump = true;
    104.             Debug.Log("coldide");
    105.         }
    106.     }
    107.  
    108.     void OnCollisionExit2D(Collision2D collision)
    109.     {
    110.         if (collision.gameObject.tag == "grass")
    111.         {
    112.             Canjump = false;
    113.             Debug.Log("not colided");
    114.         }
    115.     }
    116.  
    117.  
    118. }
     
  2. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    Start by clearing the code, it's long and hard to read.
    Code (CSharp):
    1. // Long
    2. if (controller.isdead == true)
    3. {
    4.     alive = false;
    5. }
    6. else
    7. {
    8.     alive = true;
    9. }
    10.  
    11. // Short
    12. alive = !controller.isdead;
    Code (CSharp):
    1. // Long
    2. if (Input.GetKey(KeyCode.D))
    3. {
    4.     Dpress = true;
    5. }
    6. else
    7. {
    8.     Dpress = false;
    9. }
    10.  
    11. if (Input.GetKey(KeyCode.A))
    12. {
    13.     Apress = true;
    14. }
    15. else
    16. {
    17.     Apress = false;
    18. }
    19.  
    20. if (Input.GetKeyDown(KeyCode.Space))
    21. {
    22.     spacedown = true;
    23. }
    24. else
    25. {
    26.     spacedown = false;
    27. }
    28.  
    29. // Short
    30. Dpress = Input.GetKey(KeyCode.D);
    31. Apress = Input.GetKey(KeyCode.A);
    32. spacedown = Input.GetKeyDown(KeyCode.Space);
    Code (CSharp):
    1. // Long
    2. if (Dpress == true && alive == true)
    3. {
    4.     rb.velocity = Vector2.right * movepower;
    5. }
    6.  
    7. if (Apress == true && alive == true)
    8. {
    9.     rb.velocity = Vector2.left * movepower;
    10. }
    11.  
    12. // Short
    13. if ((Dpress || Apress) && alive)
    14. {
    15.     rb.velocity = new Vector2((Dpress ? 1 : -1) * movepower, rb.velocity.y);
    16. }
    Edit: The final code can look like this. Try it out, maybe it solved the problem.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Playermovement : MonoBehaviour
    4. {
    5.     [SerializeField] GameObject Deathcontroll;
    6.     [SerializeField] Rigidbody2D rb;
    7.     [SerializeField] float jumppower;
    8.     [SerializeField] float movepower;
    9.     private logiccontroller controller;
    10.     private bool alive;
    11.     private bool spacedown;
    12.  
    13.     private void Start()
    14.     {
    15.         rb = GetComponent<Rigidbody2D>();
    16.         alive = true;
    17.         controller = Deathcontroll.GetComponent<logiccontroller>();
    18.     }
    19.  
    20.     private void Update()
    21.     {
    22.         alive = !controller.isdead;
    23.  
    24.         bool Dpress = Input.GetKey(KeyCode.D);
    25.         bool Apress = Input.GetKey(KeyCode.A);
    26.         spacedown = Input.GetKeyDown(KeyCode.Space);
    27.  
    28.         if (CanJump() && spacedown && alive)
    29.         {
    30.             rb.velocity = Vector2.up * jumppower;
    31.             controller.jumped = true;
    32.         }
    33.         else
    34.         {
    35.             controller.jumped = false;
    36.         }
    37.  
    38.         if ((Dpress || Apress) && alive)
    39.         {
    40.             rb.velocity = new Vector2((Dpress ? 1 : -1) * movepower, rb.velocity.y);
    41.         }
    42.  
    43.         if (controller.reset)
    44.         {
    45.             transform.position = Vector3.zero;
    46.         }
    47.     }
    48.  
    49.     private bool CanJump()
    50.     {
    51.         return Canjump && grounded;
    52.     }
    53.  
    54.     private bool grounded;
    55.  
    56.     private void OnCollisionEnter2D(Collision2D collision)
    57.     {
    58.         grounded = collision.gameObject.tag == "grass";
    59.         if (grounded)
    60.         {
    61.             Debug.Log("collided");
    62.         }
    63.     }
    64.  
    65.     private void OnCollisionExit2D(Collision2D collision)
    66.     {
    67.         if (collision.gameObject.tag == "grass")
    68.         {
    69.             grounded = false;
    70.             Debug.Log("not collided");
    71.         }
    72.     }
    73. }
    74.  
     
  3. DR_Cheese1

    DR_Cheese1

    Joined:
    Aug 25, 2023
    Posts:
    6
    It fixed my original problem but now i cant jump without enabling canjump manually
     
  4. DR_Cheese1

    DR_Cheese1

    Joined:
    Aug 25, 2023
    Posts:
    6
    And btw the reason my code is so long is becouse i am new to programming
     
  5. DR_Cheese1

    DR_Cheese1

    Joined:
    Aug 25, 2023
    Posts:
    6
    Fixed it just had to remove canjump from Return canjump && grounded
     
  6. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    Everyone had to start so it's fine. Now you can notice where you can write something shorter, especially working with booleans. Try to follow this https://learn.unity.com/pathway/junior-programmer . This will give you extra basics in most common problems. In Unit 2 I remember it was about jumping and grounding, and how to animate.
    You are right I had to skip something, it's good you already fixed this.