Comments and answers for "How to clamp rotation between negative and positive value"
http://answers.unity.com/questions/1409655/how-to-clamp-rotation-between-negative-and-positiv.html
The latest comments and answers for the question "How to clamp rotation between negative and positive value"Comment by eliteforcevn on eliteforcevn's answer
http://answers.unity.com/comments/1825245/view.html
Thanks it helped me alot :3 thanks you broSat, 27 Mar 2021 13:27:47 GMTeliteforcevnComment by Fostex on Fostex's answer
http://answers.unity.com/comments/1754324/view.html
$$anonymous$$an, you are a life saver..Thu, 23 Jul 2020 23:48:22 GMTFostexComment by unit_nick on unit_nick's comment
http://answers.unity.com/comments/1716747/view.html
If you are changing direction then force an equivalent rotation of the object you want to clamp and then apply the clamp.Fri, 10 Apr 2020 02:38:32 GMTunit_nickComment by xoox on xoox's comment
http://answers.unity.com/comments/1716737/view.html
@unit_nick Then how do I clamp the rotation from -165 degrees to 165 degrees?
It seems when I use -165, 165 for min/max angles it gives the object full range between -165 and 165 (330 degrees) but I'm trying to clamp the rotation to the same relativeRange (30 degrees) even when facing the other direction.Fri, 10 Apr 2020 01:22:08 GMTxooxComment by unit_nick on unit_nick's answer
http://answers.unity.com/comments/1716731/view.html
Because the values should never be outside the range of +/- 180. This is relative to the objects direction. Turning 195 defeats the purpose when it could just turn 165 in the other direction.Fri, 10 Apr 2020 00:56:25 GMTunit_nickAnswer by xoox
http://answers.unity.com/answers/1716723/view.html
@unit_nick This solution seems to work when facing one direction using -15 and 15 min/max angles but when my object changes direction and I try to adjust the min and max angles to 165 and 195 the object can't go above 180 degrees and gets locked to the min 165 angle. Do you know why this could be happening?Fri, 10 Apr 2020 00:25:44 GMTxooxComment by unit_nick on unit_nick's comment
http://answers.unity.com/comments/1409836/view.html
Actually this way might be cleaner.
Vector3 angles = turretToClamp.turret.transform.eulerAngles;
angles.z = $$anonymous$$athf.Clamp(((angles.z + 540) % 360) - 180, min, max);
turretToClamp.turret.transform.eulerAngles = angles;Wed, 20 Sep 2017 19:34:25 GMTunit_nickComment by Nemarques on Nemarques's comment
http://answers.unity.com/comments/1409807/view.html
Thank you very much!Wed, 20 Sep 2017 17:57:16 GMTNemarquesComment by unit_nick on unit_nick's comment
http://answers.unity.com/comments/1409778/view.html
Sure.
Relative range is half the whole range. This is so we can convert the rotation to a relative +/- which means we only have to test for 1 maximum value
Because the ranges aren't equal on either side an offset is required to make the relative values equal
The turret angle z value is then converted to its relative offset value
If the absolute (positive) value of this relative offset value of z is greater than half the whole range then it must be outside either the min or max value. In which case we limit it to half the whole range. Then relRange is multiplied by the sign of z so that relRange is now a negative value if z is negative, or a positive value if z is positive. The the offset that was removed is put back so as to account for the ranges not being equal.
Lastly the original angle with the modified z axis is returned to its transform so as to update its rotation.Wed, 20 Sep 2017 16:37:46 GMTunit_nickComment by Nemarques on Nemarques's answer
http://answers.unity.com/comments/1409764/view.html
Hey thanks for the answer but could u explain a little bit more about what you have done?Wed, 20 Sep 2017 16:11:41 GMTNemarquesAnswer by unit_nick
http://answers.unity.com/answers/1409708/view.html
public float minAngle = -20;
public float maxAngle = 40;
private void Update()
{
// get relative range +/-
float relRange = (maxAngle - minAngle) / 2f;
// calculate offset
float offset = maxAngle - relRange;
// convert to a relative value
Vector3 angles = turretToClamp.turret.transform.eulerAngles;
float z = ((angles.z + 540) % 360) - 180 - offset;
// if outside range
if (Mathf.Abs(z) > relRange)
{
angles.z = relRange * Mathf.Sign(z) + offset;
turretToClamp.turret.transform.eulerAngles = angles;
}
}Wed, 20 Sep 2017 14:00:56 GMTunit_nick