- Home /
Why does it flip me around and around (First Person)
I had this working a few weeks ago and I have and used the same code in another project and it flips me around and around. The 'Player' is a empty game object and has a capsule collider, rigid body attached to it and the character controller attached to it plus this code. (the 'player' also within its folder thingy/under the 'player' is the camera and 3d object capsule connected to it) Any help would be most appreciated! Code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
[SerializeField] Transform playerCamera = null;
[SerializeField] private float mouseSensitivity = 3.5f;
[SerializeField] private float movementSpeed;
[SerializeField] private float walkSpeed;
[SerializeField] private float runSpeed;
[SerializeField] private float runBuildUpSpeed;
[SerializeField] KeyCode runKey;
[SerializeField] float gravity = -25.0f;
[SerializeField] [Range(0.0f, 0.5f)] float moveSmoothTime = 0.3f;
[SerializeField] [Range(0.0f, 0.5f)] float mouseSmoothTime = 0.03f;
[SerializeField] bool lockCursor = true;
private float cameraPitch = 0.0f;
private float velocityY = 0.0f;
CharacterController controller = null;
Vector2 currentDir = Vector2.zero;
Vector2 currentDirVelocity = Vector2.zero;
Vector2 currentMouseDelta = Vector2.zero;
Vector2 currentDeltaVelocity = Vector2.zero;
private void Start()
{
controller = GetComponent<CharacterController>();
if (lockCursor)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
private void Update()
{
UpdateMouseLook();
UpdateMovement();
}
private void UpdateMouseLook()
{
Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentDeltaVelocity, mouseSmoothTime);
cameraPitch -= currentMouseDelta.y * mouseSensitivity;
cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);
playerCamera.localEulerAngles = Vector3.right * cameraPitch;
transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity);
}
private void UpdateMovement()
{
Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
targetDir.Normalize();
currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime);
if (controller.isGrounded)
velocityY = 0.0f;
velocityY += gravity * Time.deltaTime;
Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * movementSpeed + Vector3.up * velocityY;
controller.Move(velocity * Time.deltaTime);
SetMovementSpeed();
}
private void SetMovementSpeed()
{
if (Input.GetKey(runKey))
movementSpeed = Mathf.Lerp(movementSpeed, runSpeed, Time.deltaTime * runBuildUpSpeed);
else
movementSpeed = Mathf.Lerp(movementSpeed, walkSpeed, Time.deltaTime * runBuildUpSpeed);
}
}
Answer by Hazsha · Dec 09, 2021 at 05:38 AM
Try Constraint rotation in rigidbody in which axis you want to clamp
IF IT'S NOT WORKING THEN CHECK PLAYER.FBX IMPORT SETTING AND ANIMATOR ROOT MOTION
Thanks! By Constraining all in the rotation worked great!
Again very much appreciated thanks!
Your answer
Follow this Question
Related Questions
Crosshair? How? 6 Answers
FPS Controller Question Again !! 1 Answer
Night-vision/thermal possible? 5 Answers