- Home /
Player's Movement not smooth
When my player jumps and lands on something it always shakes around like that. I tried using a collider and I don't see anything in the movement script that may cause this. any solutions?
Player Movement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public static int movespeed = 6;
public Vector3 userDirection = Vector3.right;
public Rigidbody rb;
public bool isGrounded;
Vector3 jumpMovement;
bool jump = false;
[SerializeField]
float jumpHeight = 1.8f, jumpSpeed = 8f;
void Start()
{
rb = GetComponent<Rigidbody>(); }
void OnCollisionStay()
{
isGrounded = true;
}
public float fallMultiplier = 3.5f;
public float lowJumpMultiplier = 2f;
void Update()
{
//movement
transform.Translate(userDirection * movespeed * Time.deltaTime);
if (Input.GetButton("Jump") && !jump)
StartCoroutine(Jump());
}
IEnumerator Jump()
{
float originalHeight = transform.position.y;
float maxHeight = originalHeight + jumpHeight;
jump = true;
yield return null;
while (transform.position.y < maxHeight)
{
transform.position += transform.up * Time.deltaTime * jumpSpeed;
yield return null;
}
while (transform.position.y > originalHeight)
{
transform.position -= transform.up * Time.deltaTime * jumpSpeed;
yield return null;
}
rb.useGravity = true;
jump = false;
yield return null;
}
}
Answer by Namey5 · Apr 06, 2020 at 05:55 AM
If you are using a rigidbody then you really shouldn't be handling movement directly through the transform. The reason this is happening is because when your character jumps, the height that they started the jump at is stored. So long as the character is above that height, the jump coroutine will continue to run. In this case, even though they have landed, the script still thinks they are in a state of jumping because they are above where they originally jumped from, so it will keep jumping. I would suggest either just using the rigidbody (by adjusting velocity, forces, constraining rotation, etc.), or switching to a proper character controller that is built for this kind of thing.