- Home /
Question by
Binnebro · Apr 28, 2020 at 08:37 AM ·
jumprigidbody.addforcejumping object
When I jump i teleport up and then glide down
I know it is because of the VelocityChange that is being applied, but I dont know how to fix it.
Here's the code using UnityEngine;
public class PlayerMovement : MonoBehaviour { public float maxSpeed = 200f; public Rigidbody rb; public float forwardForce = 2f; public bool isGrounded = false; public float jumpPower = 20f; public float gravityForce = 10f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
Move();
if (Input.GetKey("space") && isGrounded == true)
{
rb.AddForce(new Vector3(0, jumpPower, 0), ForceMode.Impulse);
isGrounded = false;
}
if (rb.velocity.magnitude > maxSpeed)
{
rb.velocity = rb.velocity.normalized * maxSpeed;
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "jumpable")
{
isGrounded = true;
}
else
{
isGrounded = false;
}
}
void Move()
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime, ForceMode.VelocityChange);
}
}
Comment
Your answer
Follow this Question
Related Questions
2D Horizontal trampoline/spirng issues 1 Answer
Another Slow Fall Problem... 2 Answers
Jump character with click on space key in Javascript 1 Answer