- Home /
Rigidbody keeps floating when move in jump
I've tried everrything to make the character fall down right after a jump, but if I just jump everything works fine, but if I jump and then move, my character keeps floating and fall down extremely slow
Here's my code for the controller:
public class ThirdPersonController : MonoBehaviour
{
private Inputs _input;
private Vector3 PlayerMovementInput;
private float turnSmoothTime = 0.1f;
private float turnSmoothVelocity;
[SerializeField] private Rigidbody PlayerBody;
[Space]
[SerializeField] private float Speed;
[SerializeField] private float JumpForce;
[SerializeField] private Transform cam;
[SerializeField] private Transform FeetPosition;
public bool Grounded = true;
public LayerMask GroundLayers;
// Start is called before the first frame update
void Start()
{
_input = GetComponent<Inputs>();
}
// Update is called once per frame
void FixedUpdate()
{
PlayerMovementInput = new Vector3(_input.move.x, 0f, _input.move.y);
IsGrounded();
MovePlayer();
}
private void MovePlayer()
{
if (Input.GetKeyDown(KeyCode.Space) && Grounded)
{
PlayerBody.AddForce(Vector3.up * JumpForce, ForceMode.Impulse);
}
Vector3 direction = new Vector3(PlayerMovementInput.x, 0f, PlayerMovementInput.z).normalized;
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(PlayerBody.transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
PlayerBody.transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
PlayerBody.velocity = moveDir * Speed;
}
}
private void IsGrounded()
{
Grounded = Physics.CheckSphere(FeetPosition.position,0.1f,GroundLayers);
}
}
Answer by hsnzkg · Oct 05, 2021 at 03:14 AM
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
PlayerBody.velocity = moveDir * Speed;
After calculating the moveDir check the y axis velocity its zero i guess so after finding moveDir create another temp direction Vector3 and leave the Y parameter like what is currently Y velocity. Because you are stopping the -Y velocity values with assingning to it 0.
Then assign the final move direction to the Rigidbody.
I hope I could help
You were right, thanks for your help. What I did was to keep the original velocity in the Y Axis and done!
Your answer
Follow this Question
Related Questions
Can i make more gripped rigidbody sphere Controllers 0 Answers
Best Jump Source: Rigidbody or CharController 0 Answers
Why wont my character jump? (using rigidbody and addForce) 1 Answer
How to use character controller to push down hinge joint properly 1 Answer
Step Offset for Rigidbody Character 1 Answer