My character keeps snapping to where it’s meant to jump. I want to smooth it out, but nothing has worked. I’ve used gravity, I’ve tried Mathf and Vector3 lerps, and so many other things I’ve lost track. I figured there was a universal way of doing this, but I’ve found absolutely nothing useful to me online that doesn’t require the use of a CharacterController or Rigidbody. Also, this only started happening when I realized my character wasn’t rotating in the direction of movement. When I fixed that, the Jump() method no longer worked and I’ve been trying to adjust it since. This has gone on for at least a week. This is where I am now:
void Update()
{
GroundChecking();
Movement();
Jump();
}
#region Movement Method
private void Movement()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 move = new Vector3(horizontal, 0f, vertical);
Transform camera = Camera.main.transform;
Vector3 direction = Vector3.zero;
direction = camera.right * horizontal + camera.forward * vertical;
float directionMagnitude = direction.magnitude; //replace magnitude with speed cap
direction.y = 0f;
direction = direction.normalized * directionMagnitude;
if(direction != Vector3.zero)
{
transform.rotation = Quaternion.LookRotation(direction);
transform.position += direction * speed * Time.deltaTime;
}
}
#endregion
#region Ground Checking
private void GroundChecking()
{
liftPoint = new Vector3(0f, 1 + sphereCastRadius, 0f);
Ray ray = new Ray(transform.TransformPoint(liftPoint), Vector3.down);
RaycastHit hit = new RaycastHit();
float sphereCastMaxDistance = 20f;
_isGrounded = false;
if (Physics.SphereCast(ray, sphereCastRadius, out hit, sphereCastMaxDistance, discludeCharacters) && hit.distance <= 2f)
{
_isGrounded = true;
}
else
{
_isGrounded = false;
}
}
#endregion
#region Jumping
private void Jump()
{
if (_isGrounded && Input.GetKeyDown(KeyCode.Space))
{
transform.position += Vector3.up * jumpSpeed * Time.deltaTime;
_jumpInput = true;
}
if (!_isGrounded && !_jumpInput)
transform.position += -Vector3.up * Time.deltaTime;
if (!_isGrounded && _jumpInput)
{
transform.position += -Vector3.up * Time.deltaTime;
if (transform.position.y >= jumpHeight)
{
_jumpInput = false;
}
}
}
#endregion
First of all, big fan of how you lay out your work. Makes it easy to read and understand. But lets start and talk about how you are handling position at the moment.
transform.position += Vector3.up * jumpSpeed * Time.deltaTime;
The problem with that is you are setting the max position. As a rule of thumb, you should always avoid setting the position. Especially for online games. Rather let Unity handle the position;
Just tell it where the start and/or end positions should be.
public float gravity = 8.91f;
void Jump () {
// Create a new directional vector
var moveDirection = Vector3.Zero;
// Check if it is grounded
if (isGrounded) {
if (Input.GetKeyDown(KeyCode.Space)) {
moveDirection.y = jumpSpeed;
}
else {
moveDirection.y = 0.0;
}
}
else {
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
}
transform.translate(moveDirection);
}