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 Jumping while walking

Discussion in '2D' started by mathieu548, Jan 6, 2021.

  1. mathieu548

    mathieu548

    Joined:
    Jan 21, 2014
    Posts:
    10
    Hello I wrote a player script to move and my main problem is I can't walk and jump simultaneously my script is inspired from others but when I press right or left it doesn't jump on the contrary when I jump I can move left or right but I'm falling slower than when not moving. (rb angular set to 0 gravity to 1) And the animation takes time to play even when exit time is 0
    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class PlayerController : MonoBehaviour
    8. {
    9.     public Rigidbody2D rb;
    10.     public float walkSpeed = 10f;
    11.     private BoxCollider2D[] boxes;
    12.     public BoxCollider2D me;
    13.     public BoxCollider2D weapon;
    14.     private bool jump;
    15.     private bool attack;
    16.     public Animator playerAnimator;
    17.     private Vector2 moveVelocity;
    18.     private Vector2 moveVelocityj;
    19.     public float jumpHeight = 10f;
    20.     public PlayerManager playerManager;
    21.  
    22.     private void Start()
    23.     {
    24.         playerAnimator = GetComponentInChildren<Animator>();
    25.         rb = GetComponent<Rigidbody2D>();
    26.         boxes = GetComponents<BoxCollider2D>();
    27.         for (int i = 0; i < boxes.Length; i++)
    28.         {
    29.             me = boxes[0];
    30.             weapon = boxes[1];
    31.         }
    32.  
    33.     }
    34.     // Update is called once per frame
    35.     private void Update()
    36.     {
    37.         if (Input.GetButtonDown("Jump") && isGrounded())// sur place
    38.         {
    39.             playerAnimator.SetBool("Walk", false);
    40.             playerAnimator.SetTrigger("Jump");
    41.             jump = true;
    42.             Vector2 vector2j = new Vector2(0, rb.position.y + jumpHeight);
    43.             moveVelocityj = vector2j * jumpHeight;
    44.             //rb.MovePosition(rb.position + moveVelocityj * Time.deltaTime);
    45.             rb.velocity=(Vector2.up*jumpHeight);
    46.         }
    47.         if (Input.GetButtonUp("Jump") && !isGrounded())
    48.         {
    49.             jump = false;
    50.             playerAnimator.SetBool("Walk", false);
    51.         }
    52.  
    53.         Vector2 vector2 = new Vector2(Input.GetAxisRaw("Horizontal"), 0);
    54.         if ((vector2.x !=0) && (playerManager.death!=true))
    55.         {            playerAnimator.SetBool("Walk",true);
    56.         moveVelocity = vector2 * walkSpeed;
    57.  
    58.             Vector2 localRot = new Vector2(0f, (Input.GetAxisRaw("Horizontal") <= 0) ? 180f : 0f);
    59.             transform.eulerAngles = localRot;
    60.             rb.MovePosition(rb.position + moveVelocity * Time.deltaTime);
    61.         }
    62.         if (!isGrounded()||vector2.x==0)
    63.         {
    64.             playerAnimator.SetBool("Walk", false);
    65.         }
    66.         if (Input.GetButtonDown("Jump") && isGrounded() && vector2.x!=0)// sur place
    67.         {
    68.             Vector2 localRot = new Vector2(0f, (Input.GetAxisRaw("Horizontal") <= 0) ? 180f : 0f);
    69.             transform.eulerAngles = localRot;
    70.             rb.MovePosition(rb.position + moveVelocity * Time.deltaTime);
    71.             playerAnimator.SetBool("Walk", false);
    72.             playerAnimator.SetTrigger("Jump");
    73.             jump = true;
    74.             Vector2 vector2j = new Vector2(0, rb.position.y + jumpHeight);
    75.             moveVelocityj = vector2j * jumpHeight;
    76.             //rb.MovePosition(rb.position + moveVelocityj * Time.deltaTime);
    77.             rb.velocity = (Vector2.up * jumpHeight);
    78.             moveVelocity = vector2 * walkSpeed;
    79.  
    80.         }
    81.         if (Input.GetButtonDown("Fire1"))
    82.         {            playerAnimator.SetTrigger("Attack");
    83.             playerAnimator.SetBool("Walk", false);
    84.             attack = true;
    85.             weapon.enabled = true;
    86.             //animation attack
    87.  
    88.         }
    89.         if (Input.GetButtonUp("Fire1"))
    90.         {
    91.             playerAnimator.SetBool("Walk", false);
    92.             attack = false;
    93.             weapon.enabled = false;
    94.             //animation attack
    95.  
    96.         }
    97.  
    98.         if (playerManager.death)
    99.         {
    100.             playerAnimator.SetBool("Walk", false);
    101.             playerAnimator.SetTrigger("Death");
    102.         }
    103.  
    104.  
    105.     }
    106.  
    107.     private bool isGrounded()
    108.     {
    109.        if(transform.position.y<=0.0f)
    110.         {
    111.             return true;
    112.         }
    113.         return false;
    114.     }
    115.    
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    So your script is very confusing and there is just too much jammed in there. Look up clean code practices to help you a lot going forward. As for your issue, its very hard to tell since you are doing so much in your jump If block. You are doing rb.MovePosition on line 70 and then doing a lot of weird things on line 74, 75, 77 and 78. All of those line are affecting the Rigidbody2D and it really should be as simple as something like rb.AddForce(Vector2.Up * jumpForce) for example.

    Try thing great, easy video from Brackeys to get a nice and simple Movement and Jump script going. Make sure to watch it in its entirety. After you get that working, you can add in your Fire1 Attack and such. I highly recommend redoing your entire movement script as it will save you hours of frustration in the long run.
     
  3. programmatic01

    programmatic01

    Joined:
    Jul 7, 2023
    Posts:
    1
    Code (CSharp):
    1. I want to walk and jump at the same time, how is that
    2. [B][COLOR=#00b300]Like That : [/COLOR][COLOR=#4d4dff]https://youtu.be/8HkQtoo2HmI[/COLOR][/B]
    3. public class Player : MonoBehaviour
    4. {
    5.     public float movespeed;
    6.     public float JumpForce;
    7.     private Animator Anim;
    8.     private Rigidbody2D rb;
    9.     private bool canDoubleJump;
    10.     private float movinginput;
    11.     public LayerMask whatisGround;
    12.     public float GroundCheckdistance;
    13.     private bool isGrounded;
    14.     public float jumpgo;
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         rb = GetComponent<Rigidbody2D>();
    19.         Anim = GetComponent<Animator>();
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         bool isMoving = rb.velocity.x != 0;
    26.         Anim.SetBool("isMoving", isMoving);
    27.         CollisionChecks();
    28.         InputChecks();
    29.  
    30.         if (isGrounded)
    31.         {
    32.             canDoubleJump = true;
    33.         }
    34.  
    35.         if (isGrounded)
    36.         Move();
    37.         if (Input.GetKeyDown(KeyCode.RightArrow))
    38.         {
    39.             Jump();
    40.         }
    41.  
    42.  
    43.     }
    44.  
    45.     private void InputChecks()
    46.     {
    47.         movinginput = Input.GetAxisRaw("Horizontal");
    48.         if (Input.GetKeyDown(KeyCode.Space))
    49.         {
    50.  
    51.             JumpButton();
    52.         }
    53.         //if (Input.GetKeyDown(KeyCode.RightArrow))
    54.         //{
    55.         //    Jump();
    56.         //}
    57.  
    58.     }
    59.  
    60.     private void JumpButton()
    61.     {
    62.         if (isGrounded)
    63.         {
    64.             Jump();
    65.         }
    66.     }
    67.  
    68.     private void Move()
    69.     {
    70.         rb.velocity = new Vector2(movespeed * movinginput, rb.velocity.y);
    71.     }
    72.     //private void JumpGo()
    73.     //{
    74.     //    rb.velocity = new Vector2(rb.velocity.x, JumpForce);
    75.  
    76.     //}
    77.     private void Jump()
    78.     {
    79.         rb.velocity = new Vector2(rb.velocity.x, JumpForce);
    80.     }
    81.     private void CollisionChecks()
    82.     {
    83.         isGrounded = Physics2D.Raycast(transform.position, Vector2.down, GroundCheckdistance, whatisGround);
    84.     }
    85.  
    86.     private void OnDrawGizmos()
    87.     {
    88.         Gizmos.DrawLine(transform.position, new Vector2(transform.position.x, transform.position.y - GroundCheckdistance));
    89.     }
    90. }