Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Jump height issues with character controller

Discussion in 'Scripting' started by JackFranklin96, Apr 13, 2024.

  1. JackFranklin96

    JackFranklin96

    Joined:
    Jan 21, 2015
    Posts:
    16
    So I've given my character the ability to move and jump with the code below but when I go to jump my character doesn't jump as high as subsequent jumps also it keeps landing and jumping when the button is held but getkeydown doesn't work

    [code = CSharp]
    using NUnit.Framework;
    using System.Collections;
    using System.Collections.Generic;
    using System.Security.Cryptography.X509Certificates;
    using Unity.VisualScripting;
    using UnityEngine;
    public class CharacterMovement : MonoBehaviour
    {
    [SerializeField] float MovementSpeed = 1.0f;
    [SerializeField] float JumpHeight = 1.0f;
    bool isJumping;
    private void OnCollisionEnter2D(Collision2D other)
    {
    if (other.gameObject.CompareTag("Floor"))
    {
    isJumping = false;
    }
    }
    private void OnCollisionExit2D(Collision2D other)
    {
    if (other.gameObject.CompareTag("Floor"))
    {
    isJumping = true;
    }
    }
    void Update()
    {
    if (Input.GetKey(KeyCode.A))
    {
    transform.Translate(-MovementSpeed * Time.deltaTime ,0,0);
    }
    if (Input.GetKey(KeyCode.D))
    {
    transform.Translate(MovementSpeed * Time.deltaTime, 0, 0);
    }
    Rigidbody2D rigidbody2D = GetComponent<Rigidbody2D>();
    if (Input.GetKey(KeyCode.Space) && isJumping == false)
    {
    rigidbody2D.AddForce(new Vector2(0, JumpHeight));
    }
    }
    }

    [/code]
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,054
    Hi!,
    Transform.Translate isn't intended for a rigidbody.
    Code (CSharp):
    1. using UnityEngine;
    2. public class Basic2DController : MonoBehaviour
    3. {
    4. Rigidbody2D rb;
    5. RaycastHit2D[] hits=new RaycastHit2D[5];
    6. bool jump;
    7.     void Start()
    8.     {
    9.        rb=GetComponent<Rigidbody2D>();
    10.     }
    11.  
    12.     void Update()
    13.     {
    14.         if (Input.GetButtonDown("Jump"))
    15.             jump=true;
    16.     }
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         if (jump && rb.Cast(Vector2.down,hits,0.05f)>0) // Want to jump and we're standing on something?
    21.         {
    22.             rb.AddForce(new Vector2(0,10-rb.velocity.y),ForceMode2D.Impulse); // add the jump force while also cancelling out our current vertical velocity. This gives a constant jump height
    23.             jump=false;
    24.         }
    25.         rb.AddForce(new Vector2(Input.GetAxisRaw("Horizontal")*100,0));
    26.     }
    27. }
     
    Last edited: Apr 14, 2024