- Home /
Smoother movement and jumping with rigidbodies
I keep trying to make smoother movement and jumping with a rigidbody but wither the jumping stops working or the movement isn't smooth. And the charactercontroller is not working like I want it to.
private Vector3 move;
public float velY, gravity, jumpSpeed;
public BoxCollider col;
public float speed;
public Rigidbody playerR;
public static bool jumped;
public Joystick joystick;
public void Start()
{
jumpSpeed = 7;
gravity = 7;
speed = 4;
velY = 5;
}
void Update()
{
if (IsGrounded())
{
velY -= gravity * Time.deltaTime;
}
Vector3 move = Vector3.zero;
move.x = Input.GetAxisRaw("Horizontal") * speed;
move.y = velY;
move.z = Input.GetAxisRaw("Vertical") * speed;
if (joystick.InputDirection != Vector3.zero)
{
move.x = joystick.InputDirection.x * speed;
move.z = joystick.InputDirection.z * speed;
move.y = 0;
}
playerR.velocity = move;
}
public bool IsGrounded()
{
int layerMask = LayerMask.GetMask("Platform");
return Physics.CheckBox(col.bounds.center, new Vector3(col.bounds.center.x, col.bounds.min.y, col.bounds.center.z), col.transform.rotation, layerMask);
}
Few things:
Duplication is bad. Ins$$anonymous$$d of multiplying every variable by 'speed', just do it once where you're setting your velocity.
Vector3 move = Vector3.zero;
move.x = Input.GetAxisRaw("Horizontal");
move.y = velY;
move.z = Input.GetAxisRaw("Vertical");
if (joystick.InputDirection != Vector3.zero)
{
move.x = joystick.InputDirection.x;
move.z = joystick.InputDirection.z;
move.y = 0;
}
playerR.velocity = move * speed;
You should also handle physics in FixedUpdate ins$$anonymous$$d of Update. Just move playerR.velocity = move;
out of Update into FixedUpdate.
To fix the issues you brought up, some more details would be helpful. How isn't it smooth and how isn't it working like you want it to?
Answer by Cornelis-de-Jager · May 08, 2019 at 09:33 PM
If you want smoother movement then its best to let Unity handle velocity. Rather than setting the velocity just add force.
Rigidbody rb;
public float jumpStrength;
void Start () {
rb = GetComponent<Rigidbody>();
}
void LateUpdate () {
// Handle the movement
HandleMovement();
// Handle Jumping
HandleJump();
}
void HandleMovement () {
// Get the movement vector
Vector3 move = new Vector3 (Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
move *= speed * Time.DeltaTime;
// Use ForceMode to make it smoother
// Force is the default but you can also try
// Acceleration & VelocityChange a bit more sharper movement
// We must also ensure that we don't add force to exceed max velocity/speed
var movementPlane = new Vector3 (rb.velocity.x, 0, rb.velocity.z);
if (movementPlane.magnitude < speed)
rb.AddForce(move, ForceMode.Force);
}
void HandleJump () {
// For jumping we do something similar with force
// However we want it instant so we can use the Impulse force mode
if (Grounded()){
rb.AddForce(new Vector3 (0, jumpStrength, 0), ForceMode.Impulse);
}
}
This works but my jumping still looks laggy, i think its the way my camera moves that's causing it
@RoyaleNite below is a script I use for smooth camera movement.
public class CameraScript : $$anonymous$$onoBehaviour {
public Transform player;
public Vector3 offset;
public float delay = 1f;
void Start () {
offset = transform.position - player.position;
}
void LateUpdate () {
transform.position = Vector3.Lerp(transform.position, transform.position + offset, delay);
}
}
Its a very simple script but works very well. If you want it smoother then simply increase the delay. If you want to make it more advance to add zoo$$anonymous$$g and rotation around the player, simply change the offset variable to whatever you like.
Your answer
Follow this Question
Related Questions
Rigidbody--Addforce on a Spherical Platform(A Globe) 2 Answers
Help with player movement and jumping 0 Answers
I need help with a movement script 1 Answer
Rigidbody.Addforce makes object skip 5 Answers