- Home /
Question by
Chof1TheCreator · May 20 at 07:55 PM ·
scripting problemcharacter controller
hello, so my question is about why my players head sticks to the ceiling, i am using empty object to detect floor collision for gravity.
public class PlayerMovement : MonoBehaviour {
public CharacterController controller;
float speed;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
void start()
{
speed = 7;
}
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if(Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
if(Input.GetKey(KeyCode.LeftShift) == false)
{
speed = 7;
}
else if(isGrounded)
{
speed = 10;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to minimize an input value? 0 Answers
Why is my Character staying in Air Idle rather than Grounded Idle? 0 Answers
Check if player is grounded and play a sound 1 time 1 Answer
Merry Fragmas Character Controller for Unity 5 0 Answers
Script for syncing Oculus VR player position with VR headset not working as intended 1 Answer