I have a character in unity controlled by a rigidbody. However, the jump is one jerky movement not a smooth motion. Here is my script:
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
public class Controller : MonoBehaviour
{
[Tooltip("Rigidbody component attached to the player")]
public Rigidbody rb;
private float movementX;
private float movementY;
private float gravity = -9.81f;
private float speedX = 10;
private float speedY = 1000;
private float speedZ = 5;
private bool isJumping = false;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
}
void OnCollisionEnter(Collision collision)
{
// SceneManager.LoadScene(1);
}
// Update is called once per frame
void OnMove(InputValue movementValue)
{
Vector2 movementVector = movementValue.Get<Vector2>();
movementX = movementVector.x;
movementY = movementVector.y;
}
void OnJump()
{
//isJumping = true;
Vector3 movementVelocity = new Vector3();
movementVelocity.y = Mathf.Sqrt(speedY * -3.0f * gravity);
movementVelocity.y *= 100;
rb.AddForce(movementVelocity, ForceMode.Force);
//isJumping = false;
}
void CalculateMovement()
{
Vector3 movementVelocity = new Vector3(movementX * speedX, 0, speedZ);
rb.velocity = movementVelocity;
}
void FixedUpdate()
{
CalculateMovement();
}
}
Is there a way to get a smooth jump with a rigidbody?
I have tried all the different options for ForceMode
and tried updating the Rigidbody.velocity
characteristic, both at the same time and separately to the movement.
CodePudding user response:
Try using AddForce(), thats easier and always works as well. Your code will give a smooth jump too, but AddForce() is more about Unity physics.
CodePudding user response:
For smooth transitions you would ordinarily use interpolation, or lerping. Interpolation allows you to smooth out the effect of running physics at a fixed frame rate. Interpolate is an Rigidbody' s property and is is turned off by default, try to enabled it.