Limiting localrotation between 2 values
Hey all, I've been spinning wheels on this for a while now, my aim is to lock my localRotation of my little tool arm between my min and max yRotation values.
The commented out code partially worked but ended up having snapping issues when out of the assigned range. Here's some code.
public float traverseSpeed;
public float dist;
public Plane plane; public Quaternion rotation;
public float minRotation;
public float maxRotation;
public float mathClamp; public Vector3 localAngles;
public float yRotationMin;
public float yRotationMax;void Update(){ //Current
Ray ray = camera.main.ScreenPointToRay(Input.mousePosition);
plane = new Plane (Vector3.up, transform.position);if (plane.Raycast (ray, out dist)) {
rotation = Quaternion.LookRotation (ray.GetPoint (dist) - transform.position); >Debug.Log ("Greater and less than");
transform.rotation = Quaternion.RotateTowards (transform.rotation, rotation, traverseSpeed * Time.deltaTime);
//NEW //Take 2
/*localAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, transform.localEulerAngles.z);
localAngles.y = Mathf.Clamp (transform.localRotation.y, yRotationMin, yRotationMax);
transform.localRotation = Quaternion.Euler (localAngles);*/
/* //Take 1
if (localAngles.y > yRotationMin && localAngles.y < yRotationMax) { transform.rotation = Quaternion.RotateTowards (transform.rotation, rotation, traverseSpeed * Time.deltaTime);
}
if (localAngles.y < yRotationMin) {
transform.rotation = Quaternion.RotateTowards (transform.localRotation, minRotation, traverseSpeed * Time.deltaTime); }
if (localAngles.y > yRotationMax) {
transform.rotation = Quaternion.RotateTowards (transform.localRotation, maxRotation, traverseSpeed * Time.deltaTime);
}*/ //} //New } }
Any help getting a smooth result would be greatly appreciated!
Answer by Meany747 · Nov 01, 2018 at 06:09 AM
void Update(){ //Current
Ray ray = camera.main.ScreenPointToRay(Input.mousePosition);
plane = new Plane (Vector3.up, transform.position);
if (plane.Raycast (ray, out dist)) {
rotation = Quaternion.LookRotation (ray.GetPoint (dist) - transform.position); >Debug.Log ("Greater and less than");
transform.rotation = Quaternion.RotateTowards (transform.rotation, rotation, traverseSpeed * Time.deltaTime);
At the moment this is the working code, above there are 2 attempts of limiting the rotation outside of the y axis limits to no avail.
Your answer
Follow this Question
Related Questions
Rotate player with mouse. 0 Answers
How to make Rotation of HingeJoint2D constant via mouse button C# 1 Answer
Camera rotation with mouse cursor 0 Answers
How would I add rotation limits to this? 1 Answer
Help with weapon rotation top down 2D C# 0 Answers