- Home /
Interpolate problem? Jump isn't smooth...
Hi, I have got a problem with jump... It's not smooth as I want it.. and I haven't got the idea how to fix it. Here is a video with my problem and my code. https://www.youtube.com/watch?v=Im1Vfr7TIwI&feature=youtu.be
public class Viva_Movement_Rigidbody : MonoBehaviour {
[Header("Movement Variables")]
public float moveSpeed = 5.0f;
[Range(0.0f, 10.0f)]
public float jumpSpeed;
public float rotateSpeed = 10.0f;
public bool isGrounded = false;
[Header("Acceleration&Decceleration")]
public float accSpeed = 2.0f;
public float deccSpeed = 0.5f;
public float airDeccSpeed = 0.2f;
[Header("Other Variables")]
public float height = 0.5f;
public LayerMask ground;
Rigidbody rb;
float currSpeed = 0.0f;
float deadzone = 0.19f;
float angle;
Vector2 input;
Transform cam;
Quaternion targetRotatiton;
private void Start()
{
rb = GetComponent<Rigidbody>();
cam = Camera.main.transform;
}
private void Update()
{
Debug.Log("currSpeed: " + currSpeed);
GetInput();
CalculateDirection();
if (Mathf.Abs(input.x) < deadzone && Mathf.Abs(input.y) < deadzone)
{
Decceleration();
Move();
}else
{
Acceleration();
Move();
Rotate();
}
}
private void FixedUpdate()
{
if (Input.GetButtonDown("XboxA") && isGrounded)
{
Jump();
}
GroundCheck();
}
void GetInput()
{
input = new Vector2(Input.GetAxisRaw("XboxHorizontal"), Input.GetAxisRaw("XboxVertical"));
if (input.magnitude < deadzone)
input = Vector2.zero;
else
input = input.normalized * ((input.magnitude - deadzone) / (1 - deadzone));
}
void CalculateDirection()
{
angle = Mathf.Atan2(input.x, input.y);
angle = Mathf.Rad2Deg * angle;
angle += cam.eulerAngles.y;
}
void Move()
{
transform.position += transform.forward * currSpeed * Time.deltaTime;
}
void Rotate()
{
targetRotatiton = Quaternion.Euler(0, angle, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotatiton, rotateSpeed * Time.fixedDeltaTime);
}
void Jump()
{
rb.velocity = new Vector3(rb.velocity.x, jumpSpeed * 40, rb.velocity.z) * Time.fixedDeltaTime;
//rb.AddForce(Vector3.up * jumpSpeed * 40 * Time.deltaTime, ForceMode.Impulse);
}
void Acceleration()
{
currSpeed += accSpeed;
if (currSpeed >= moveSpeed)
currSpeed = moveSpeed;
}
void Decceleration()
{
if (isGrounded)
currSpeed -= deccSpeed;
else if (!isGrounded)
currSpeed -= airDeccSpeed;
if (currSpeed <= 0)
currSpeed = 0;
}
void GroundCheck()
{
if (Physics.Raycast(transform.position, Vector3.down, height, ground))
isGrounded = true;
else
isGrounded = false;
}
}
Thanks for every idea how to fix it.
You're controlling position and velocity at the same time, so you might end up with weird movement unless you're sure neither of those are stepping on the other's toes.
Usually, people will have velocity / physics control their vertical motion and let player inputs control the horizontal / depth axes. In this case, you're moving your object horizontally on the Update cycle, which doesn't run at the same time as FixedUpdate. As a result, your object seems to stutter, because rather than getting a bunch of instructions to move sideways and vertically at the same time, it's getting things unevenly.
Try adjusting your $$anonymous$$ove() to use Time.fixedDeltaTime and see if moving that function call to FixedUpdate smooths things out.
Your answer
Follow this Question
Related Questions
GetButtonDown is unresponsive 0 Answers
Unity Physics On Input Issue? 0 Answers
RigidBody immediately stops after AddForce 1 Answer
Force to Velocity scaling? 2 Answers