Question by
Alec_E · Apr 14, 2021 at 10:39 PM ·
scripting problemmovementphysicscontroller
How can I fix my jumping script?
I made a jump script based on a tutorial, however, it's really bad, the jump height is inconsistent and I fall really slow. Is there a better way of doing this?`
using UnityEngine;
public class RoblotMoves : MonoBehaviour
{
public Rigidbody rb;
public float jumpPower = 10f;
public float gravity = 20f;
public bool cubeOnTheGround = true;
// Start is called before the first frame update
private void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Jump")&& cubeOnTheGround)
{
rb.AddForce(new Vector3(0, jumpPower * Time.deltaTime, 0), ForceMode.Impulse);
cubeOnTheGround = false;
if(transform.position.y > 1)
{
rb.AddForce(new Vector3(0, -gravity * Time.deltaTime, 0), ForceMode.Impulse);
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Floor")
cubeOnTheGround = true;
}
}
Comment