❔ Unity clamp rotation
hello, i need to limit a rotation relative to another rotation and i don't know how to do it, i've already tried a lot of things. i need something like
but there are always problems with quaternoins.
I tried to fix it but nothing worked for me. only u works well
float angle = Mathf.Clamp(target.rotation.x, reference.rotation.x-maxAngle, reference.rotation.x+maxAngle);
float angle = Mathf.Clamp(target.rotation.x, reference.rotation.x-maxAngle, reference.rotation.x+maxAngle);
public Transform target;
public Transform reference;
public Vector2 maxAngle = new Vector2(30f, 30f);
public Vector2 targetAngle = new Vector2(30f, 30f);
void Update()
{
Vector3 clampedAngle = new Vector3(targetAngle.x, targetAngle.y, 0);
float a = Vector3.SignedAngle(reference.forward, Quaternion.Euler(targetAngle) * Vector3.forward, reference.right);
float inverseX = reference.eulerAngles.x < 0 ? -1 : 1;
float inverseY = reference.eulerAngles.y < 0 ? 1 : -1;
float inverseXDist = WrapAngle(Mathf.Abs(a)) >= 90f ? -1 : 1;
float inverseXRot = WrapAngle(Mathf.Abs(a)) >= 90f ? -180 + maxAngle.x * 2 : 0;
float inverseXRot2 = WrapAngle(Mathf.Abs(a)) >= 90f ? -180 : 0;
float x = Mathf.Clamp(WrapAngle(clampedAngle.x), WrapAngle(-maxAngle.x + Mathf.Abs(reference.eulerAngles.x) * inverseXDist + inverseXRot2), WrapAngle(maxAngle.x + Mathf.Abs(reference.eulerAngles.x)) * inverseXDist + inverseXRot);
float y = Mathf.Clamp(WrapAngle(clampedAngle.y), WrapAngle(-maxAngle.y - Mathf.Abs(reference.eulerAngles.y) * inverseY), WrapAngle(maxAngle.y + Mathf.Abs(reference.eulerAngles.y)));
target.rotation = Quaternion.Euler(x, y, 0);
}
public float WrapAngle(float angle)
{
angle = angle % 360f;
if (angle < -180f)
return angle + 360f;
else if (angle > 180f)
return angle - 360f;
else
return angle;
}
public Transform target;
public Transform reference;
public Vector2 maxAngle = new Vector2(30f, 30f);
public Vector2 targetAngle = new Vector2(30f, 30f);
void Update()
{
Vector3 clampedAngle = new Vector3(targetAngle.x, targetAngle.y, 0);
float a = Vector3.SignedAngle(reference.forward, Quaternion.Euler(targetAngle) * Vector3.forward, reference.right);
float inverseX = reference.eulerAngles.x < 0 ? -1 : 1;
float inverseY = reference.eulerAngles.y < 0 ? 1 : -1;
float inverseXDist = WrapAngle(Mathf.Abs(a)) >= 90f ? -1 : 1;
float inverseXRot = WrapAngle(Mathf.Abs(a)) >= 90f ? -180 + maxAngle.x * 2 : 0;
float inverseXRot2 = WrapAngle(Mathf.Abs(a)) >= 90f ? -180 : 0;
float x = Mathf.Clamp(WrapAngle(clampedAngle.x), WrapAngle(-maxAngle.x + Mathf.Abs(reference.eulerAngles.x) * inverseXDist + inverseXRot2), WrapAngle(maxAngle.x + Mathf.Abs(reference.eulerAngles.x)) * inverseXDist + inverseXRot);
float y = Mathf.Clamp(WrapAngle(clampedAngle.y), WrapAngle(-maxAngle.y - Mathf.Abs(reference.eulerAngles.y) * inverseY), WrapAngle(maxAngle.y + Mathf.Abs(reference.eulerAngles.y)));
target.rotation = Quaternion.Euler(x, y, 0);
}
public float WrapAngle(float angle)
{
angle = angle % 360f;
if (angle < -180f)
return angle + 360f;
else if (angle > 180f)
return angle - 360f;
else
return angle;
}
1 Reply
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.