- Home /
unity restricting rotation even though i don't want it to.
My player has a script to make it turn while flying. The script has been working well up until a short while ago, whereupon it started making it so that the player cannot rotate past a certain point in one direction and it clips to that point when rotating from the other. I don't know what I did to make it start doing this and I don't know how to make it stop.
Here is my script for player movement anyways just in case:
private float movespeed = 30.5f;
private float rotspeed = 50f;
public Rigidbody rb;
public Vector3 rotationeulerx;
public Vector3 rotationeulery;
public Vector3 rotationeulerz;
public float distanceground;
public bool isGrounded = false;
private float gravity = -6.00f;
public bool Groundcling = false;
private float Grounddown = -50;
public Camera cam;
public Vector3 forwardoffset;
RaycastHit hitinfo;
RaycastHit groundfalldir;
public Vector3 surfaceNormal;
public Vector3 forwardrelativesurfaceNormal;
public Vector3 grounddownforce;
void Start()
{
rb = GetComponent<Rigidbody>();
distanceground = GetComponent<Collider>().bounds.extents.y;
Vector3 grounddownforce = Grounddown * transform.forward;
cam = Camera.main;
}
// Update is called once per frame
void Update()
{
if (Groundcling)
{
rotspeed = 100f;
}
if (!Groundcling)
{
rotspeed = 50f;
}
rotationeulerz = new Vector3(0, 0, rotspeed);
rotationeulery = new Vector3(0, rotspeed, 0);
rotationeulerx = new Vector3(rotspeed, 0, 0);
{
if (Input.GetKey(KeyCode.Space))
{
rb.AddForce(Vector3.up * Time.deltaTime * movespeed);
}
if (Input.GetKey(KeyCode.W))
{
rb.AddForce(transform.forward * Time.deltaTime * movespeed);
}
if (Input.GetKey(KeyCode.S))
{
rb.AddForce(transform.forward * Time.deltaTime * -movespeed);
}
if (Input.GetKey(KeyCode.A))
{
rb.AddForce(transform.right * Time.deltaTime * -movespeed);
}
if (Input.GetKey(KeyCode.D))
{
rb.AddForce(transform.right * Time.deltaTime * movespeed);
}
if (Input.GetKey(KeyCode.J))
{
Quaternion deltarotation = Quaternion.Euler(-rotationeulery * Time.deltaTime);
rb.MoveRotation(rb.rotation * deltarotation);
}
if (Input.GetKey(KeyCode.L))
{
Quaternion deltaroation = Quaternion.Euler(rotationeulery * Time.deltaTime);
rb.MoveRotation(rb.rotation * deltaroation);
}
if (!Groundcling)
{
if (Input.GetKey(KeyCode.I))
{
Quaternion deltarotation = Quaternion.Euler(rotationeulerx * Time.deltaTime);
rb.MoveRotation(rb.rotation * deltarotation);
}
if (Input.GetKey(KeyCode.K))
{
Quaternion deltarotation = Quaternion.Euler(-rotationeulerx * Time.deltaTime);
rb.MoveRotation(rb.rotation * deltarotation);
}
if (Input.GetKey(KeyCode.O))
{
Quaternion deltarotation = Quaternion.Euler(-rotationeulerz * Time.deltaTime);
rb.MoveRotation(rb.rotation * deltarotation);
}
if (Input.GetKey(KeyCode.U))
{
Quaternion deltarotation = Quaternion.Euler(rotationeulerz * Time.deltaTime);
rb.MoveRotation(rb.rotation * deltarotation);
}
}
}
{ //tells me if rigidbody is on the ground
if (!Physics.Raycast(transform.position, -Vector3.up, out hitinfo, distanceground + .10f))
{
isGrounded = false;
//print("In Air");
}
if (Physics.Raycast(transform.position, -Vector3.up, out hitinfo, distanceground + .10f))
{
if (!hitinfo.collider.isTrigger)
{
isGrounded = true;
//print("On Ground");
}
}
}
{ //adds gravity when not flying
if (!isGrounded)
{
if (!Input.GetKey(KeyCode.Space))
{
rb.AddForce(Vector3.up * gravity * Time.deltaTime);
}
}
}
{
forwardoffset = Vector3.Cross(transform.right, hitinfo.normal);
}
{
Debug.DrawLine(transform.position, transform.position + forwardoffset * 2, Color.blue);
}
//Physics.Raycast(transform.position, -Vector3.up, out groundfalldir, Mathf.Infinity);
CharacterfacerelativetoSurface();
}
private void FixedUpdate()
{
{
if (rb == isGrounded)
{
Groundcling = true;
// print("on it");
}
if (Input.GetKey(KeyCode.Space))
{
Groundcling = false;
}
if (Groundcling == true)
{
// print("on ground");
}
{
if (Groundcling)
{
if (!isGrounded)
{
rb.AddForce(0, Grounddown * Time.deltaTime, 0);
//rb.AddForce(transform.up * Grounddown * Time.deltaTime);
//print("space difference");
//surfaceNormal = groundfalldir.normal;
//rb.AddForce(surfaceNormal * Grounddown * Time.deltaTime);
}
}
}
}
}
private void CharacterfacerelativetoSurface()
{
if (Physics.Raycast(transform.position, -Vector3.up, distanceground + .1f))
{
if (!Input.GetKey(KeyCode.J))
{
if (!Input.GetKey(KeyCode.L))
{
RaycastHit mousePosition;
//Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(Input.mousePosition, Vector3.down, out mousePosition, Mathf.Infinity);
//surfaceNormal = hitinfo.normal;
//Quaternion targetRotation = Quaternion.FromToRotation(Vector3.up, surfaceNormal);
//Vector3 mousePosInLocalSpace = Quaternion.Inverse(targetRotation) * (mousePosition.point - transform.position);
//float turnByAngle = Mathf.Atan(mousePosInLocalSpace.x * -90) * Mathf.Rad2Deg;
//targetRotation = targetRotation * Quaternion.AngleAxis(turnByAngle, surfaceNormal);
//I assume I am meant to add this last line in below as it wont rotate without rb.MoveRotation.
//rb.MoveRotation(Quaternion.LerpUnclamped(transform.rotation, targetRotation, Time.deltaTime * 10));
surfaceNormal = hitinfo.normal;
forwardrelativesurfaceNormal = Vector3.Cross(transform.right, surfaceNormal);
Quaternion targetrotation = Quaternion.LookRotation(forwardrelativesurfaceNormal, surfaceNormal);
rb.MoveRotation(Quaternion.LerpUnclamped(transform.rotation, targetrotation, Time.deltaTime * 10));
//rb.MoveRotation(Quaternion.LerpUnclamped(transform.rotation, forwardrotate, Time.deltaTime));
}
}
}
if (Groundcling)
{
if (!isGrounded)
{
if (Physics.Raycast(transform.position, - Vector3.up, out groundfalldir, distanceground = .3f))
{
if (!Input.GetKey(KeyCode.J))
{
if (!Input.GetKey(KeyCode.L))
{
surfaceNormal = groundfalldir.normal;
forwardrelativesurfaceNormal = Vector3.Cross(transform.right, surfaceNormal);
Quaternion targetrotation = Quaternion.LookRotation(forwardrelativesurfaceNormal, surfaceNormal);
rb.MoveRotation(Quaternion.LerpUnclamped(transform.rotation, targetrotation, Time.deltaTime * 10));
}
}
}
}
}
It may be down to gimbal lock. Rotation should really be done via quaternions and not Euler angles. There's a lot of different rotations being applied there so I'm not sure which rotation is being applied when you get the issue.
Your answer
Follow this Question
Related Questions
Upgrading to Unity 5 messed up my Rigidbody Center of masses 1 Answer
Freeze Gameobject's position on collision 0 Answers
the object of type rigidbody has been destroyed 3 Answers
RigidBody immediately stops after AddForce 1 Answer
How can I pragmatically move a navmesh rigid body when changing scenes? 0 Answers