pppoe252110
pppoe252110
CC#
Created by pppoe252110 on 7/27/2023 in #help
❔ 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
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);
but there are always problems with quaternoins.
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;
}
I tried to fix it but nothing worked for me. only u works well
2 replies
CC#
Created by pppoe252110 on 7/2/2023 in #help
❔ get names of all monitors in unity
I want to get the names of all monitors to display them in the settings menu
8 replies
CC#
Created by pppoe252110 on 5/28/2023 in #help
❔ I need help to implement linear interpolation into my marching squares algorithm (Unity)
2 replies