The title is very self-explanatory. The issue is that, whenever I jump, the player's jump height is very strange and random. I am basically making a platformer for a game jam and this issue is not very nice for platforming lol.
Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed = 10f;
private Rigidbody2D rb;
public float jumpForce = 800f;
private bool allowJump;
public Transform point;
public LayerMask whatIsGround;
public float radius = 0.5f;
private void Start()
{
rb = GetComponent();
}
private void Update()
{
float x = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(x * speed * Time.deltaTime, rb.velocity.y);
allowJump = Physics2D.OverlapCircle(point.position, radius, whatIsGround);
if (Input.GetButtonDown("Jump") && allowJump == true)
{
rb.AddForce(Vector2.up * jumpForce * Time.deltaTime, ForceMode2D.Impulse);
}
else
{
allowJump = false;
}
}
}
so basically, I have some simple horizontal movement and some basic jumping. I have a bool that detects the ground. If it is overlapping, it will allow the player to jump, if there is nothing overlapping, player cannot jump so as to stop the player from jumping in mid-air.
Pls help :)
↧