- Home /
im making an endless runner but my capsule starts floating after i jump. any help?
im making an endless runner but whenever i jump the capsule floats above the platforms. I used character controller for the movement.
Here's the code for the character controller:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class herocharactercontroller : MonoBehaviour
{
[SerializeField] LayerMask groundLayers;
[SerializeField] private float runspeed = 8f;
[SerializeField] private float jumpHeight = 2f;
private float gravity = -50f;
private CharacterController characterController;
private Vector3 velocity;
private bool isGrounded;
private float HorizontalInput;
void Start()
{
characterController = GetComponent<CharacterController>();
}
void Update()
{
HorizontalInput = 1;
//face forward
transform.forward = new Vector3(HorizontalInput, 0, Mathf.Abs(HorizontalInput) - 1);
//isGrounded
isGrounded = Physics.CheckSphere(transform.position, 0.1f, groundLayers, QueryTriggerInteraction.Ignore);
if (isGrounded && velocity.y < 0)
{
velocity.y = 0;
}
else
{
//add gravity
velocity.y += gravity * Time.deltaTime;
}
characterController.Move(new Vector3(HorizontalInput * runspeed, 0, 0) * Time.deltaTime);
if (isGrounded && Input.GetButtonDown("Jump"))
{
velocity.y += Mathf.Sqrt(jumpHeight * -2 * gravity);
}
//vertical velocity
characterController.Move(velocity * Time.deltaTime);
}
}
Comment
Your answer
Follow this Question
Related Questions
How to make an upgrading game? 1 Answer
My projects don't open! 3 Answers
does unity run in the back ground? 1 Answer
Character Controller visibility in Unity 5 0 Answers
[HELP WANTED] Blender FBX to Unity Animation Issue 0 Answers