- Home /
Question by
cruxfn · Apr 19, 2021 at 09:42 AM ·
fps controller
I am new to unity and I've started working on my own RigidBody FPS controller. I'm using sphere cast for ground check. when I crouch my grounded return's false even though its clearly grounded. PLS HELP. This is my entire movement code.
using UnityEngine;
public class PlayerController : MonoBehaviour { //components private Rigidbody rb; public Transform orientation; public Transform playerCam;
//movement
public float moveSpeed;
[SerializeField] private float moveMultiplyer;
[Range(0, 10)] public float sensitivity;
private float sensMultiplyer = 0.1f;
public float counterForce;
private float counterMultiplyer = 100f;
private float extraGravity = 100f;
private float maxSpeed = 10f;
//input
private Vector3 direction = new Vector3(0f, 0f, 0f);
//jump
private bool jumping;
public float jumpForce;
private float downForce = -10f;
//crouch
private Vector3 crouchScale = new Vector3(1f, .5f, 1f);
private Vector3 standScale = new Vector3(1f, 1.5f, 1f);
private bool crouching;
private bool crouchEnabled;
//look
private float desiredX;
private float xRotation;
//groundCheck
public LayerMask whatIsGround;
private float groundCheckRadius = .6f;
private float groundCheckDistance = 1f;
private bool grounded;
//slopes
private bool OnSlope;
private RaycastHit slopeHit;
private Vector3 slopeDir;
//multiplyer
private float airMultiplyer = 0.2f;
private float groundMultiplyer = 1000f;
private float crouchMultiplyer = 0f;
private float jumpMultiplyer = 5f;
private float slopeMultiplyer = 0f;
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
private void Start()
{
transform.localScale = standScale;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = true;
}
private void Update()
{
ControlSpeed();
grounded = Physics.SphereCast(transform.position, groundCheckRadius, Vector3.down, out RaycastHit hitInfo, groundCheckDistance, whatIsGround);
onSlope();
Debug.Log("ON SLOPE :"+ onSlope());
Debug.Log("GROUNDED :" + grounded);
PlayerInput();
PlayerLook();
if (jumping && grounded)
{
Jump();
}
}
private void FixedUpdate()
{
PlayerMovement();
}
/// <summary>
/// player's inputs
/// </summary>
private void PlayerInput()
{
direction.x = Input.GetAxisRaw("Horizontal");
direction.y = Input.GetAxisRaw("Vertical");
direction.Normalize();
jumping = Input.GetKeyDown(KeyCode.Space);
crouching = Input.GetKeyDown(KeyCode.LeftControl);
if (crouching)
{
StartCrouch();
}
if (Input.GetKeyUp(KeyCode.LeftControl))
{
StopCrouch();
}
}
/// <summary>
/// look around with mouse and clamping max angle in the x axis
/// </summary>
private void PlayerLook()
{
float mouseX = Input.GetAxis("Mouse X") * sensitivity * sensMultiplyer;
float mouseY = Input.GetAxis("Mouse Y") * sensitivity * sensMultiplyer;
Vector3 rot = playerCam.transform.localRotation.eulerAngles;
desiredX = rot.y + mouseX;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
playerCam.transform.rotation = Quaternion.Euler(xRotation, desiredX, 0);
orientation.transform.rotation = Quaternion.Euler(0, desiredX, 0);
}
private void Jump()
{
rb.velocity += Vector3.up * jumpForce * jumpMultiplyer;
}
/// <summary>
/// adding forces to move the player
/// </summary>
private void PlayerMovement()
{
rb.AddForce(Vector3.down * extraGravity * Time.fixedDeltaTime);
rb.AddForce(orientation.transform.forward * direction.y * moveSpeed * Time.fixedDeltaTime * moveMultiplyer);
rb.AddForce(orientation.transform.right * direction.x * moveSpeed * Time.fixedDeltaTime * moveMultiplyer);
CounterMovement(-rb.velocity);
}
private void StartCrouch()
{
this.transform.localScale = crouchScale;
transform.position = new Vector3(transform.position.x, transform.position.y - 0.5f, transform.position.z);
crouchEnabled = true;
}
private void StopCrouch()
{
this.transform.localScale = standScale;
transform.position = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
crouchEnabled = false;
}
/// <summary>
/// controlling sloppy movement
/// </summary>
private void CounterMovement(Vector3 dir)
{
if (rb.velocity.magnitude != 0)
{
rb.AddForce(dir * counterForce * counterMultiplyer * Time.fixedDeltaTime);
}
if (rb.velocity.y < 0)
{
rb.velocity += Vector3.down * Physics2D.gravity.y * (downForce - 1) * Time.deltaTime;
}
}
/// <summary>
/// Controlling player's speed and reset player's speed when above max speed
/// </summary>
private void ControlSpeed()
{
//changing movement multiplyer if player is grounded
if (grounded && this.transform.localScale == standScale && !jumping)
{
moveMultiplyer = groundMultiplyer;
}
if (!grounded)
{
moveMultiplyer = groundMultiplyer * airMultiplyer;
}
if (crouchEnabled && !onSlope())
{
moveMultiplyer = groundMultiplyer * crouchMultiplyer;
}
//stop player from going over maxSpeed
Vector3 v = rb.velocity;
v.y = 0f;
v = Vector3.ClampMagnitude(v, maxSpeed);
v.y = rb.velocity.y;
rb.velocity = v;
}
private bool onSlope()
{
if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, 1.5f+ 0.5f))
{
if (slopeHit.normal != Vector3.up)
{
if (this.transform.localScale == standScale)
{
moveMultiplyer = groundMultiplyer;
}
return true;
}
else
{
return false;
}
}
return false;
}
}
Comment