I have been making a game, and for some reason, my whole character seems to rotate when I move my mouse, sometimes only my camera moves. I had this problem since I first started this project but I somehow fixed it somehow, but it does come up sometimes, like for example when I disable my character movement script or something. Right now, the issue has returned in full scale and I can't seem to control my character, the character seems to rotate around a circle from a fixed center, that is just intuition but it does feel like it when I move my camera around, I didn't even do anything, it was working fine yesterday. And I want to resolve it permanently.
Here are some details regarding this - A video showing the issue (above), current unity version and associated scripts. -
First, here's my player hierarchy - https://ibb.co/pjrcvZDr
"Player" being an empty game object, Character is the actual game object which has the scripts and the Character controller and under the character is the main camera.
Also here are images for the character controller and the scripts in the hierarchy -
- https://ibb.co/VYMJRSwv
- https://ibb.co/FLyvXsTX
Current Version - Unity 6.4 (6000.4.8f1)
The Scripts (I am a beginner so scripts may be badly written)
1. Character Movement Script -
using UnityEngine;
using UnityEngine.TextCore.Text;
public class PlayerMovement : MonoBehaviour
{
[Header("Player Settings")]
[SerializeField] float normalSpeed = 3f;
[SerializeField] float sprintSpeed = 5f;
private float speed;
//References
private CharacterController characterController;
[SerializeField] private VelocityManagement velocityManagement;
private PlayerData playerData;
[SerializeField] AudioClip grassWalking;
[SerializeField] AudioClip houseWalking;
[SerializeField] float sprintPitch = 1f;
[SerializeField] float normalPitch = 0.7f;
//Privates
float movementX;
float movementZ;
Vector3 move;
private float velocity;
private AudioSource walking;
void Start()
{
characterController = GetComponent<CharacterController>();
playerData = GetComponent<PlayerData>();
walking = GetComponent<AudioSource>();
}
void Update()
{
if (playerData.isMobile)
{
movementX = Input.GetAxisRaw("Horizontal");
movementZ = Input.GetAxisRaw("Vertical");
move = transform.forward * movementZ + transform.right * movementX;
move = move.normalized;
move.y = velocityManagement.velocity;
AudioClip walkingClip = playerData.isInside ? houseWalking : grassWalking;
walking.clip = walkingClip;
if (Input.GetKey(KeyCode.LeftShift))
{
speed = sprintSpeed;
walking.pitch = sprintPitch;
}
else
{
speed = normalSpeed;
walking.pitch = normalPitch;
}
if (movementX != 0 || movementZ != 0)
{
if (!walking.isPlaying)
{
walking.Play();
}
}
else
{
if (walking.isPlaying)
{
walking.Stop();
}
}
characterController.Move(move * speed * Time.deltaTime);
}
}
}
2. Camera Rotation Script -
using UnityEngine;
public class CameraRotation : MonoBehaviour
{
[Header("Camera Settings")]
[SerializeField] float sensitivity = 4f;
[Header("References")]
[SerializeField] Transform playerBody;
[SerializeField] private PlayerData playerData;
//Privates
private float mouseX;
private float mouseY;
private float xRotation;
private Vector3 originalLocalPosition;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
originalLocalPosition = transform.localPosition;
}
void Update()
{
//Upward/Downward Movement
if (playerData.isMobile)
{
mouseX = Input.GetAxis("Mouse X") * sensitivity;
mouseY = Input.GetAxis("Mouse Y") * sensitivity;
playerBody.rotation = Quaternion.Euler(0, playerBody.rotation.eulerAngles.y + mouseX, 0);
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90, 90);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
}
}
}
3. Velocity Management Script -
using UnityEngine;
public class VelocityManagement : MonoBehaviour
{
[Header("Settings")]
[SerializeField] float gravity = -19.6f;
//References
private CharacterController characterController;
[SerializeField] private Transform spawnPoint;
//Privates
public float velocity;
private Vector3 move;
void Start()
{
characterController = GetComponent<CharacterController>();
}
void Update()
{
if (characterController.isGrounded && velocity < 0)
{
velocity = -2f;
}
velocity += gravity * Time.deltaTime;
if (characterController.transform.position.y < -10f)
{
characterController.transform.position = spawnPoint.position + Vector3.up * characterController.height/2;
characterController.transform.rotation = spawnPoint.rotation;
}
}
}
4. Player Data Script -
using UnityEngine;
public class PlayerData : MonoBehaviour
{
[Header("Player State")]
public bool isInside = false;
public bool isMobile = true;
public bool onPC = false;
}
That is all, if anyone can help me, please do. If you guys need further details/information, I can share my Discord.
Upvote1Downvote0Go to comments1RepostShare