Question by
Killhome221 · Oct 22, 2019 at 04:24 PM ·
scripting problem3dcamera-look
Character turning around itself when looked up or down!
I opened a post before and i didnt get any answers idk if its because its in the wrong forum but here we go. So i've been trying to replicate a game to start my coding adventures. I have looked up tutorial videos on youtube and learnt a lot of things. Now my problem is i made a character it has legs, arms, a head and a body i made camera looking script so right arm and head will rotate with the body and when i look up they will look too. And thats where the problem begins. I made a 90 degrees lock so i will not be able to look up or down more than 90 degrees. So it works kinda because when the body hits 90 degrees it turns around itself and i make a 180 quick turn and i cant really fix it. Heres the code and i would love to get some help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
[SerializeField]
Transform player, playerArmJ, playerHead;
[SerializeField]
float MouseSensitivity;
float xLimit = 0;
void Update()
{
Cursor.lockState = CursorLockMode.Locked;
CamRot();
}
void CamRot()
{
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
float rotAmountX = mouseX * MouseSensitivity;
float rotAmountY = mouseY * MouseSensitivity;
xLimit -= rotAmountY;
Vector3 rotPlayerArmJ = playerArmJ.transform.rotation.eulerAngles;
Vector3 rotPlayer = player.transform.rotation.eulerAngles;
Vector3 rotplayerHead = playerHead.transform.rotation.eulerAngles;
rotplayerHead.x -= rotAmountY;
rotplayerHead.z = 0;
rotPlayerArmJ.x -= rotAmountY;
rotPlayerArmJ.z = 0;
rotPlayer.y += rotAmountX;
playerHead.rotation = Quaternion.Euler(rotplayerHead);
playerArmJ.rotation = Quaternion.Euler(rotPlayerArmJ);
player.rotation = Quaternion.Euler(rotPlayer);
if (xLimit > 90)
{
xLimit = 90;
rotplayerHead.x = 0;
rotPlayerArmJ.x = 0;
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612213758im_/https://answers.unity.com/themes/thub/images/avi.jpg)