Question by
wyndonelbryan · Apr 01 at 12:10 AM ·
c#charactercontrollerclipping
CharacterController clipping into floor when making contact with any raised area
I'm trying to make a character controller but whenever my character comes in contact with a raised area (wall, step, etc) it sinks into the floor and you have to repeatedly jump to get it out. Heres my code, please tell me what I'm doing wrong (I'm not following any tutorials and also I'm relatively new to coding)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float sensitivity;
Vector2 rotation = Vector2.zero;
private CharacterController player;
public float moveSpeed;
public float velocity;
public float radius;
public bool isGrounded;
public float directionY;
public Transform center;
public float gravity;
public float jumpHeight;
public float curJump;
void Awake()
{
player = GetComponent<CharacterController>();
}
void Update()
{
isGrounded = Physics.CheckSphere(center.position, radius);
if(isGrounded)
{
directionY = 0;
}
if(Input.GetButtonDown("Jump") && isGrounded)
{
directionY = jumpHeight;
}
if(isGrounded == false)
{
directionY -= gravity * Time.deltaTime;
}
player.Move(new Vector3(0f, directionY, 0f) * Time.deltaTime);
rotation.y += Input.GetAxis ("Mouse X");
transform.eulerAngles = (Vector2)rotation * sensitivity;
if(Input.GetKey(KeyCode.W))
{
player.Move(transform.forward * moveSpeed);
}
if(Input.GetKey(KeyCode.S))
{
player.Move(-transform.forward * moveSpeed);
}
if(Input.GetKey(KeyCode.A))
{
player.Move(-transform.right * moveSpeed);
}
if(Input.GetKey(KeyCode.D))
{
player.Move(transform.right * moveSpeed);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
MoveTowards is curving for no reason 0 Answers
How to crouch a FPS Controller? 2 Answers
Character's jumping mechanism stuck into something invisible 1 Answer
Jump over obstacles(runner game) by speech recognition 1 Answer
Help with 2D platformer (score),For my 2D game sometimes coins get added twice to the score?! 0 Answers