Search Unity

Characters can't jump

Discussion in '2D' started by Chemicalboy, Jan 16, 2020.

  1. Chemicalboy

    Chemicalboy

    Joined:
    Nov 13, 2019
    Posts:
    6
    I'm kinda new to Unity and coding and I have been struggling with this same problem for quite some time.
    Code (CSharp):
    1. public abstract class Character : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     private float speed;
    5.     private Vector2 direction;
    6.     protected Rigidbody2D myRigidbody;
    7.     protected bool IsGrounded;
    8.     public Transform feetPosition;
    9.     public float checkRadius;
    10.     public LayerMask whatsIsGround;
    11.     [SerializeField]
    12.     protected float JumpHeight;
    13.  
    14.  
    15.  
    16.     public Vector2 Direction { get => direction; set => direction = value; }
    17.     public float Speed { get => speed; set => speed = value; }
    18.  
    19.     protected virtual void Start()
    20.     {
    21.         myRigidbody = GetComponent<Rigidbody2D>();
    22.     }
    23.     private void FixedUpdate()
    24.     {
    25.  
    26.         Move();
    27.     }
    28.     public void Move()
    29.     {
    30.         myRigidbody.velocity = Direction.normalized * Speed;
    31.      
    32.     }
    33. }
    so this is my character class and it is used by 2 players both of them look like this but of course using different keys
    Code (CSharp):
    1. public class Player_1 : Character
    2. {
    3.  
    4.    protected void Update()
    5.     {
    6.         IsGrounded = Physics2D.OverlapCircle(feetPosition.position, checkRadius, whatsIsGround);
    7.         GetInput();
    8.     }
    9.  
    10.     private void GetInput()
    11.     {
    12.         Direction = Vector2.zero;
    13.  
    14.         if (Input.GetKey(KeyCode.LeftArrow))
    15.         {
    16.             Direction += Vector2.left;
    17.         }
    18.         if (Input.GetKey(KeyCode.RightArrow))
    19.         {
    20.             Direction += Vector2.right;
    21.         }
    22.         if (IsGrounded == true && Input.GetKeyDown(KeyCode.UpArrow))
    23.         {
    24.             myRigidbody.velocity = Vector2.up * JumpHeight;
    25.         }
    26.  
    27.     }
    28. }
    Any idea what's problem?
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,464
    Try adding in some Debug.Logs and find out if certain things are being called or not.
     
  3. Chemicalboy

    Chemicalboy

    Joined:
    Nov 13, 2019
    Posts:
    6
    I have tried but nothing seems to be off :(
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,464
    Have you set the JumpHeight in the inspector? Are you sure Grounded is flagging to true?
     
  5. Chemicalboy

    Chemicalboy

    Joined:
    Nov 13, 2019
    Posts:
    6
    Yes, I have tried many values for the JumpHeight in the inspector and it doesn't work. I have debugged the IsGrounded and it is ok. Could my problem somehow be caused by the multiplayer aspect?
     
  6. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,464
    Its possible. I am not familiar with multiplayer but it could be a factor. Can your player move at all or is it just Jumping?
     
  7. Chemicalboy

    Chemicalboy

    Joined:
    Nov 13, 2019
    Posts:
    6
    It is only jumping, left/ right movement is as it should
     
  8. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,464
    I only have one last suggestion, but maybe try increasing JumpHeight to a very large number. On mine I ended up having a high mass on the player and had to make my variable a very high number to even notice the code was working and the player was indeed jumping, it just wasnt noticeable. If that doesnt work then its probably a multiplayer issue and I cannot help. Sorry I havent been much help anyways
     
  9. Chemicalboy

    Chemicalboy

    Joined:
    Nov 13, 2019
    Posts:
    6
    I got the characters jump when I had typo fixedUpdate instead of FixedUpdate but as a result characters lost their left/right movement.
    Code (CSharp):
    1.   public void Move()
    2.     {
    3.       myRigidbody.velocity = Direction.normalized * Speed;
    4.      
    5.     }
    I think this is the problem them but I have absolutely no idea why. Any ideas?
     
  10. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,464
    So i think i know why but not 100%. I had similar issues and it was because I was setting the velocity on both the jump and movement so they were conflicting and only one would work. I ended up changing my jump code to Rigidbody2d.AddForce() instead and it helped. Maybe give that a shot but I am not entirely sure.