I have a script that need to limit the player rotation but when i change the max value above 90 like 100 it doent work anymore, its stuck at 90 degrees like its the clamp limit or something.
heres my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class characterMovement : MonoBehaviour
{
public CharacterController _CharacterController;
Vector3 _moveDirection = Vector3.zero;
public GameObject _player;
public int _speed;
public int _sprintSpeed;
public int _Gravity;
public int _jumpSpeed;
public GameObject _cameraRotate;
public int _minAngle;
public int _maxAngle;
void Start()
{
}
void Update()
{
if (_CharacterController.isGrounded)
{
_moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
_moveDirection = transform.TransformDirection(_moveDirection);
_moveDirection *= _speed;
if (Input.GetButton("Jump"))
{
_moveDirection.y = _jumpSpeed;
}
}
_moveDirection.y -= _Gravity * Time.deltaTime;
_CharacterController.Move(_moveDirection * Time.deltaTime);
//camera
if (Input.GetAxis("Mouse X") < 0)
{
_player.transform.Rotate(0, -4, 0);
}
if (Input.GetAxis("Mouse X") > 0)
{
_player.transform.Rotate(0, 4, 0);
}
//up down roration
if (Input.GetAxis("Mouse Y") < 0)
{
_cameraRotate.transform.Rotate(-4, 0, 0);
}
if (Input.GetAxis("Mouse Y") > 0)
{
_cameraRotate.transform.Rotate(4, 0, 0);
}
//lock rotation
//_________broblem zone__________
float _newx = Mathf.Clamp(_cameraRotate.transform.localEulerAngles.x, _minAngle, _maxAngle); //when _maxAngle is higher than 90 it doent work because the debug.log on line 63 says its stuck on 90 degrees
_cameraRotate.transform.localEulerAngles = new Vector3(_newx, 0, 0);
Debug.Log(_newx);
}
}
I can assure you it’s not ‘Clamp’ with the problem. The code behind it is very simple:
/// <summary>
///
/// <para>
/// Clamps a value between a minimum float and maximum float value.
/// </para>
///
/// </summary>
/// <param name="value"/><param name="min"/><param name="max"/>
public static float Clamp(float value, float min, float max)
{
if ((double) value < (double) min)
value = min;
else if ((double) value > (double) max)
value = max;
return value;
}
This is the source for Mathf.Clamp, not a whole lot to it.
…
Also, why the heck is EVERY variable starting with an ‘_’?
Like, some people (myself included) use the ‘_’ to define a field that is private/protected. But you just have every field and variable, even those scoped inside a function, starting with an underscore? Is there a reason to such madness?
…
Anyways… I’m betting your issue is to do with gimbal overlap, let me play with it… confirm for myself that is. BRB.
You’ll see that when it reaches 90f around x; y and z flip to 180 each, and x starts to go back down… because 80,180,180 is the same as 100,0,0.
This all has to do with the fact that Unity stores rotation with a Quaternion, and not Euler angles. It merely converts to euler, but conversion isn’t accurate because for any given orientation there’s multiple euler rotation values that can equal it. It’s actually one of the reasons Quats are preferred… it doesn’t suffer from these oddities that eulers do (poor Euler, great mathematician, died blind and a pauper, and is remembered on large for gimbal lock).
Anyways… I’m on Japan time right now due to crunch time and my partner lives there, so I’m running on like god knows how long with out sleep… I’d love to give a lesson on quats (other members could vouch it is one of the many things I write wall posts about for new comers), but I honestly don’t have the energy right now to help you resolve your issue.
Maybe someone else can come along and expend the energy.
OR
Someone who knows eulers (I suck at eulers… srsly… I prefer quats on so many levels), might give you a hack way to resolve it in that way instead.
okay, i got it to work… butt… when the lock activate the camera tilt a little bit
hers my lock script part thing bla bla bla: _cameraRotate.transform.rotation = Quaternion.Euler(_cameraRotate.transform.rotation.eulerAngles.x, _minAngle, _maxAngle);
EDIT: it looks like the x , y, and z axis are getting changed in some weard reason
The general rules are 1) don’t read individual axes for transform.eulerAngles (e.g. eulerAngles.x is affected by eulerAngles.y and/or .z and will therefore not necessarily give a consistent result), and 2) track rotation yourself directly and apply it, rather than attempting to read/modify/apply. (Also just use transform.eulerAngles, no point using transform.rotation.eulerAngles.) In other words treat rotation as write-only in most cases.