I’m a bit new to Unity. My FPS Controller sometimes starts rotating randomly out of control when it moves or crouches, only stopping when I face a certain direction. I’ve tried looking into the RigidBody component to see if one of the axis rotations besides Y were unfreezed. I’ve also checked if the controller’s children had colliders and despite the fact that there were none, my controller still went out of control. I also considered looking into three C# scripts:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Controller_01 : MonoBehaviour
{
Player_Input_01 playerInput_01;
Rigidbody mybody;
public float speed = 2.5f;
public float walkSpeed;
public float runSpeed;
public float crouchSpeed;
void Start()
{
playerInput_01 = GetComponent<Player_Input_01>();
mybody = GetComponent();
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxis(“Horizontal”);
float vertical = Input.GetAxis(“Vertical”);
{
Vector3 moveDirection = new Vector3(horizontal, 0f, vertical) * speed * Time.deltaTime;
transform.Translate(moveDirection);
if (Input.GetKeyDown(playerInput_01.crouchKey))
{
speed = crouchSpeed;
}
if (Input.GetKeyUp(playerInput_01.crouchKey))
{
speed = walkSpeed;
}
if(Input.GetKeyDown(playerInput_01.sprintKey))
{
speed = runSpeed;
}
if(Input.GetKeyUp(playerInput_01.sprintKey))
{
speed = walkSpeed;
}
}
}
void FixedUpdate ()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_fpsCam_01 : MonoBehaviour
{
Vector2 mouseLook;
public float sensitivity;
GameObject character;
// Start is called before the first frame update
void Start()
{
character = this.transform.parent.gameObject;
}
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxis(“Mouse X”);
float vertical = Input.GetAxis(“Mouse Y”);
Vector2 look = new Vector2(horizontal, vertical);
mouseLook += look * sensitivity;
mouseLook.y = Mathf.Clamp(mouseLook.y, -80f, 80);
transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Crouch : MonoBehaviour
{
CapsuleCollider playerCol;
float originalHeight;
public float reducedHeight;
// Start is called before the first frame update
void Start()
{
playerCol = GetComponent();
originalHeight = playerCol.height;
}
// Update is called once per frame
void Update()
{
//Crouch;
if (Input.GetKeyDown(KeyCode.LeftControl))
Crouch();
else if (Input.GetKeyUp(KeyCode.LeftControl))
GoUp();
}
//Method to reduce height;
void Crouch()
{
playerCol.height = reducedHeight;
}
//Method to reset height;
void GoUp()
{
playerCol.height = originalHeight;
}
}
Here is a link to the video demonstrating this issue, showing both the scene and game view: https://www.dropbox.com/s/9434o31spv8to2l/Screen Recording (6-6-2020 7-07-38 PM).wmv?dl=0
Thank you.