I’m trying a new Unity project and i recently updated to Unity6 and I’m trying to add variable jump height to my character. I have used this code for projects in the past and it has worked but now it just does a normal jump and doesn’t have the variable jump height effect.
here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
[Header("Horizontal Movement Settings")]
private Rigidbody2D rb;
[SerializeField] private float walkSpeed = 1f;
private float xAxis;
[Header("Ground Check Settings")]
[SerializeField] private float jumpHeight = 45f;
[SerializeField] private Transform groundCheckPoint;
[SerializeField] private float groundCheckY = 0.2f;
[SerializeField] private float groundCheckx = 0.5f;
[SerializeField] private LayerMask whatIsGround;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
GetInputs();
Move();
Jump();
}
void GetInputs()
{
xAxis = Input.GetAxisRaw("Horizontal");
}
private void Move()
{
rb.linearVelocity = new Vector2(walkSpeed * xAxis, rb.linearVelocity.y);
}
public bool Grounded()
{
if(Physics2D.Raycast(groundCheckPoint.position, Vector2.down, groundCheckY, whatIsGround)
|| Physics2D.Raycast(groundCheckPoint.position + new Vector3(groundCheckx, 0, 0), Vector2.down, groundCheckY, whatIsGround)
|| Physics2D.Raycast(groundCheckPoint.position + new Vector3(-groundCheckx, 0, 0), Vector2.down, groundCheckY, whatIsGround))
{
return true;
}
else
{
return false;
}
}
void Jump()
{
if (Input.GetButtonDown("Jump") && rb.linearVelocity.y > 0)
{
rb.linearVelocity = new Vector2(rb.linearVelocity.x, 0);
}
if (Input.GetButtonDown("Jump") && Grounded())
{
rb.linearVelocity = new Vector3(rb.linearVelocity.x, jumpHeight);
}
}
}