- Home /
FPS Character keeps randomly sliding and rotating
So im trying to make my first First Person Game I have been looking for a solution for a while now, but nothing seems to help. My character keeps randomly rotating and sliding the whole time and also buggs around while walking into walls. I spent 2 hours going through websites and nothing helped. The weird thing is the rotating part. A lot of people have the sliding but not a lot seem to have the random rotate.
I have also basically tried every way how to make collisions with any kind of kombinations of settings.
This is my entire code for my Character. Mind that a lot of the code is not related to the problem.. My Camera is simply a child of the Character, thats how i made it follow it. I know you could do A TON better with that code but trying to fix the problem discribed above is my highest priority. The last method in the code is my latest attempt to fix the problem myself. I would be super happy to read your solutions, even if you say I need to change my entire code.
Felix
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
public class CharacterScript : MonoBehaviour
{
public GameObject Character;
public Rigidbody CharacterRigidbody;
public bool CurrentlyJumping = false;
public bool CurrentlyLockingMouse = false;
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 1F;
public float sensitivityY = 1F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -90F;
public float maximumY = 90F;
float rotationY = 0F;
float rotationX = 0F;
void Start()
{
LockAim();
}
void Update()
{
// CharacterRigidbody.isKinematic = false;
CheckMouseMovement();
CheckWalking();
CheckJump();
CheckAim();
CheckShoot();
CheckEscape();
if (GameObject.Find("Character").transform.position != GameObject.Find("Main Camera").transform.position)
{
GameObject.Find("Main Camera").transform.position = GameObject.Find("Character").transform.position;
}
}
public void CheckWalking()
{
// Walking
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vectors.WalkingVectorForward * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vectors.WalkingVectorBackwards * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vectors.WalkingVectorLeft * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vectors.WalkingVectorRight * Time.deltaTime);
}
}
public void CheckJump()
{
if (Input.GetKeyDown("space"))
{
CurrentlyJumping = true;
}
if (CurrentlyJumping)
{
transform.Translate(Vectors.WalkingVectorJump, Space.World);
Globals.JumpCounter = Globals.JumpCounter + 1;
if (Globals.JumpCounter > Globals.JumpDuration)
{
Globals.JumpCounter = 0;
CurrentlyJumping = false;
}
}
}
public void CheckAim()
{
//Aim
if (Input.GetKeyDown("mouse 1"))
{
if (Globals.CurrentlyAiming)
{
Globals.CurrentlyAiming = false;
}
else
{
Globals.CurrentlyAiming = true;
}
}
}
public void CheckShoot()
{
if (Input.GetKeyDown("mouse 0"))
{
}
}
public void CheckEscape()
{
if (Input.GetKeyDown("escape"))
{
LockAim();
}
}
public void LockAim()
{
if (CurrentlyLockingMouse)
{
Cursor.lockState = CursorLockMode.None;
CurrentlyLockingMouse = false;
}
else
{
Cursor.lockState = CursorLockMode.Locked;
CurrentlyLockingMouse = true;
}
}
public void CheckMouseMovement()
{
if (axes == RotationAxes.MouseXAndY)
{
rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
public void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name =="Room1")
{
// CharacterRigidbody.isKinematic = true;
CharacterRigidbody.velocity = Vector3.zero;
}
}
}
Answer by theBlueTornado · Jan 08, 2021 at 07:19 AM
try in unity lock rotation in the inspector where the Rigidbody.
Answer by Darkolos · Jan 08, 2021 at 05:41 PM
I dont know, i cant seem to find "lock rotation" anywhere in the inspector of the object the rigidbody is attached to. @theBlueTornado
Look for the Constraints dropdown in the Rigidbody component in the inspector, and tick the boxes to lock rotation on the x and z axis. @Darkolos
Your answer
Follow this Question
Related Questions
Collision Detection is too slow 0 Answers
Prevent randomGenerated Structures from colliding / clipping 1 Answer
Detect polygon? 0 Answers
OnCollisionEnter question.... 1 Answer
Rigidbody2D with box colliders passing through each other 0 Answers