Hello, I am having an issue where the player speed completely stops if you are looking fully up or down clamped at 90 degrees. the players speed also dramatically slows down when turning around very fast. I am still quite new to unity and scripting so haven’t really got an understanding of what I Am doing wrong. Any help or moderation to my code will be appreciated.
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Header("Sprinting")]
[SerializeField] private float Speed = 1.15f;
[SerializeField] private float sprintSpeed = 1.5f;
[Header("Jumping")]
[SerializeField] private float JumpHeight = 7f;
[SerializeField] private float groundcheckdistance;
[SerializeField] private float BufferCheckDistance = 0.1f;
[SerializeField] private bool PlayerGrounded = true;
[Header("Sensitivity")]
[SerializeField] private float PlayerSensitivity = 100f;
[SerializeField] private float sensMultiplier = 1f;
[Header("KeyCode Inputs")]
[SerializeField] private KeyCode JumpKey = KeyCode.Space;
[SerializeField] private KeyCode SprintKey = KeyCode.LeftControl;
[SerializeField] private KeyCode SlideKey = KeyCode.LeftShift;
private float desiredX;
private float xRotation;
public Transform playerCam;
public Transform orientation;
public Transform player;
private Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void Update()
{
PlayerDirectionalMovements();
LookRotations();
Sprinting();
Jumping();
GroundedCheck();
}
private void PlayerDirectionalMovements()
{
float HorizontalInput = Input.GetAxis("Horizontal");
float VerticalInput = Input.GetAxis("Vertical");
Vector3 CamForward = playerCam.forward;
Vector3 CamRight = playerCam.right;
CamForward.Normalize();
CamRight.Normalize();
CamForward.y = 0;
CamRight.y = 0;
Vector3 ForwardRelative = VerticalInput * CamForward;
Vector3 RightRelative = HorizontalInput * CamRight;
Vector3 MoveDir = ForwardRelative + RightRelative;
Vector3 Movement = new Vector3(MoveDir.x, 0f, MoveDir.z) * Speed;
rb.MovePosition(rb.position + Movement * Time.fixedDeltaTime);
}
private void LookRotations()
{
float mouseX = Input.GetAxis("Mouse X") * PlayerSensitivity * Time.fixedDeltaTime * sensMultiplier;
float mouseY = Input.GetAxis("Mouse Y") * PlayerSensitivity * Time.fixedDeltaTime * sensMultiplier;
Vector3 rot = playerCam.transform.localRotation.eulerAngles;
desiredX = rot.y + mouseX;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
playerCam.transform.localRotation = Quaternion.Euler(xRotation, desiredX, 0);
orientation.transform.localRotation = Quaternion.Euler(0, desiredX, 0);
}
private void Sprinting()
{
if(Input.GetKey(SprintKey) == true && PlayerGrounded)
{
Speed = sprintSpeed;
}
else
{
Speed = 1.15f;
}
}
private void Jumping()
{
if(Input.GetKeyDown(JumpKey) && PlayerGrounded)
{
rb.AddForce(new Vector3(0f, JumpHeight, 0f), ForceMode.Impulse);
PlayerGrounded = false;
}
}
private void GroundedCheck()
{
groundcheckdistance = (GetComponent<CapsuleCollider>().height / 2) + BufferCheckDistance;
RaycastHit GroundHit;
if (Physics.Raycast(transform.position, -transform.up, out GroundHit, groundcheckdistance))
{
PlayerGrounded = true;
}
else
{
PlayerGrounded = false;
}
}
}
zulo3d
August 5, 2024, 5:14pm
2
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Header("Sprinting")]
[SerializeField] private float Speed = 50;
[SerializeField] private float sprintSpeed = 100f;
[Header("Jumping")]
[SerializeField] private float JumpHeight = 7f;
[SerializeField] private float groundcheckdistance;
[SerializeField] private float BufferCheckDistance = 0.1f;
[SerializeField] private bool PlayerGrounded = true;
[Header("Sensitivity")]
[SerializeField] private float PlayerSensitivity = 100f;
[SerializeField] private float sensMultiplier = 1f;
[Header("KeyCode Inputs")]
[SerializeField] private KeyCode JumpKey = KeyCode.Space;
[SerializeField] private KeyCode SprintKey = KeyCode.LeftControl;
[SerializeField] private KeyCode SlideKey = KeyCode.LeftShift;
private float desiredX;
private float xRotation;
public Transform playerCam;
public Transform orientation;
public Transform player;
private Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void Update()
{
LookRotations();
Sprinting();
Jumping();
GroundedCheck();
}
// FixedUpdate for physics movement
void FixedUpdate()
{
PlayerDirectionalMovements();
}
private void PlayerDirectionalMovements()
{
float HorizontalInput = Input.GetAxis("Horizontal");
float VerticalInput = Input.GetAxis("Vertical");
Vector3 CamForward = playerCam.forward;
Vector3 CamRight = playerCam.right;
CamForward.Normalize();
CamRight.Normalize();
CamForward.y = 0;
CamRight.y = 0;
//Vector3 ForwardRelative = VerticalInput * CamForward;
Vector3 ForwardRelative = VerticalInput * transform.forward; // move the player along the player's forward axis, not the camera's forward axis, because the camera might be looking up or down
Vector3 RightRelative = HorizontalInput * CamRight;
Vector3 MoveDir = ForwardRelative + RightRelative;
Vector3 Movement = new Vector3(MoveDir.x, 0f, MoveDir.z) * Speed;
// Don't use MovePosition here as it's very unreliable at being blocked by collisions. MovePosition is intended for kinematic rigidbodies.
//rb.MovePosition(rb.position + Movement * Time.fixedDeltaTime);
rb.AddForce(Movement);
rb.AddForce(new Vector3(-rb.velocity.x,0,-rb.velocity.z)*5); // friction. You could remove this line and add a physics material with high friction to the rigidbody
}
private void LookRotations()
{
float mouseX = Input.GetAxis("Mouse X") * PlayerSensitivity * sensMultiplier; // don't use Time.deltaTime or Time.fixedDeltaTime here because mouse input is already frame rate independent
float mouseY = Input.GetAxis("Mouse Y") * PlayerSensitivity * sensMultiplier;
Vector3 rot = transform.localRotation.eulerAngles;
desiredX = rot.y + mouseX;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
// playerCam.transform.localRotation = Quaternion.Euler(xRotation, desiredX, 0); // this is rotating the camera around the X and Y axis.
playerCam.transform.localRotation = Quaternion.Euler(xRotation, 0, 0); // if the camera is a child of the player then you only need to rotate the camera around the x axis
//orientation.transform.rotation = Quaternion.Euler(0, desiredX, 0);
rb.MoveRotation(Quaternion.Euler(0, desiredX, 0)); // smoother rotation with this
}
private void Sprinting()
{
if(Input.GetKey(SprintKey) == true && PlayerGrounded)
{
Speed = sprintSpeed;
}
else
{
Speed = 50;
}
}
private void Jumping()
{
if(Input.GetKeyDown(JumpKey) && PlayerGrounded)
{
rb.AddForce(new Vector3(0f, JumpHeight, 0f), ForceMode.Impulse);
PlayerGrounded = false;
}
}
private void GroundedCheck()
{
groundcheckdistance = (GetComponent<CapsuleCollider>().height / 2) + BufferCheckDistance;
RaycastHit GroundHit;
if (Physics.Raycast(transform.position, -transform.up, out GroundHit, groundcheckdistance))
{
PlayerGrounded = true;
}
else
{
PlayerGrounded = false;
}
}
}
1 Like
After making these adjustments, the players sensitivity is extremely fast even when set to a low number and player speed is now extremely slow even when set to a high number. Also the player can no longer move forwards or backwards and the player object can now also clip through game objects. Do you know why any of this is happening?
zulo3d
August 5, 2024, 6:01pm
4
You’ll need to reset the script so then it uses the new default values. It’s also possible that you’ve got your player setup in an unusual way. For example, I’ve assumed the camera is a child of the player.
The Camera is the child of the player, the player has three child objects called head height, orientation, and Camera. Ill keep pestering around with some of the settings though, would you have any idea on how to fix the player being able to clip through game objects such as cubes, the player right now is currently a capsule with a collider and all the cubes have colliders on them as well.
zulo3d
August 5, 2024, 6:36pm
6
The only thing that can cause a rigidbody to clip through objects is MovePosition which I’ve commented out of your script. Did you put it back in again?
BTW - I forgot to set the default mouse sensitivity after removing the Time.fixedDeltaTime, so set it to approx 5.
zulo3d
August 5, 2024, 6:41pm
7
Oh! and change GetAxis(“Horizontal”) and GetAxis(“Vertical”) to GetAxisRaw to help make the movement a little more responsive…
I realised after I said this I deleted the movement vector 3 now its working fine but the player now has the problem where it feels like they’re skating on ice but slowly if you get what I mean, I added your friction line. any adjustments I need to make?
zulo3d
August 5, 2024, 7:35pm
9
That’s inertia and can be reduced by increasing friction. You can increase the friction amount on line 77 or you can remove the line and add a physics material with lots of friction. You can also decrease the mass setting of the rigidbody to help make it feel lighter and more responsive.
Also, see my previous post where I suggest changing GetAxis to GetAxisRaw.
1 Like