- Home /
Rigidbody.velocity seems to be breaking jumping physics
I'm currently creating a character controller script and i'm facing 2 problems:
When falling off an edge, the player falls back to the ground in an extremely slow manner. This can't have anything to do with my gravity because when I jump successfully, i'm falling normally.
Sometimes, when jumping, the player somehow gets stuck in the air right at the start and, again, falls back slowly.
The jump height is intended.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Rigidbody rb;
public Transform player;
RaycastHit hit;
public float speed;
public float force;
public bool jumping;
private void Start()
{
rb = rb.GetComponent<Rigidbody>();
}
private void Update()
{
if(Physics.Raycast(transform.position, Vector3.down, out hit, 1f))
{
jumping = false;
Debug.Log(hit.distance);
if (Input.GetKeyDown(KeyCode.Space))
{
jump();
}
}
}
private void FixedUpdate()
{
if (!jumping)
{
Vector3 move = player.transform.forward * Input.GetAxis("Vertical") + player.transform.right * Input.GetAxis("Horizontal");
move = move * speed * Time.deltaTime;
move = Vector3.ClampMagnitude(move, 20f);
rb.velocity = move;
}
}
void jump()
{
jumping = true;
rb.AddForce(force * Time.fixedDeltaTime * Vector3.up);
}
}
Video:
Your answer
Follow this Question
Related Questions
How to make rigidbodies on each side of a cube fall towards the cube? [multiple gravity / addForce] 0 Answers
Physics in 2D mode not working? 0 Answers
ForceMode.VelocityChange vs. simply adding to .velocity 0 Answers
Problems with Physics(Flipping strangely) 2 Answers
Setting a RigidBody's velocity messes with my custom gravity, not sure how to proceed. 0 Answers