Question by
Nemarques · Sep 19, 2017 at 04:13 PM ·
rotationtransformrotateclamped rotation
How can i clamp rotation between a negative value and a positive value
I want to rotate an object between -20 and 45.
but the problem is the clamp it not working with negative values and when i convert to positive values like 330 or something it has a problem with the maximum value being 45 and then it freaks out. i have tried some stuff but im not getting there this is what i have got now. Any help is appreciated thanks.
code:
Vector3 clampedTurret = new Vector3(turretToClamp.turret.transform.eulerAngles.x, turretToClamp.turret.transform.eulerAngles.y, turretToClamp.turret.transform.eulerAngles.z);
float zValue = clampedTurret.z;
if (zValue - 360 < turretToClamp.minAngle)
{
zValue = turretToClamp.minAngle + 360;
chng = true;
}
if (zValue > turretToClamp.maxAngle)
{
zValue = turretToClamp.maxAngle;
chng = true;
}
if (zValue < 0)
zValue = zValue + 360;
if (zValue > 360)
zValue = 0;
clampedTurret.z = zValue;
Comment