- Home /
Limiting rotation based on direction
Issue
Hello, So I'm trying to limit my walls from overlapping each other. So what I'm trying to do is clamp their directions to move either left or right but give them some leeway of 60 degrees. I believe this might have something to do with local rotation?
Walls
(The red walls are attached)
So In the picture, wall 1, when it goes forward like that, the red wall works 100%.
So wall 1 is at 90 degrees. The red wall is limited to 120-210 degrees, or 300-60 degrees.
Wall 2
So wall 2, it goes left. Sadly the red wall is being limited incorrectly. Currently the red wall should be able to move more left, but it does not.
Here is the code I use to limit the walls: NOTE: It is easier to read this in visual studio, or mono. It is more confusing in the webpage
//The start tower is the first tower, it faces the mouse position
startTower.transform.LookAt(endTower.transform);
//If this wall is linked to another wall
if (linked)
{
//Hit.transform is the wall that we hit with raycast, and our linked wall is attached to.
//(Right Side)
//If the wall is greater than the hit walls angle, and less than it the hit wall angle + 180
if (startTower.transform.eulerAngles.y > Clamp0360(hit.transform.eulerAngles.y) && startTower.transform.eulerAngles.y < Clamp0360(hit.transform.eulerAngles.y +180))
{
startTower.transform.eulerAngles = new Vector3(startTower.transform.eulerAngles.x, Mathf.Clamp(startTower.transform.eulerAngles.y,
Clamp0360(hit.transform.eulerAngles.y + 30), Clamp0360(hit.transform.eulerAngles.y + 150)), startTower.transform.eulerAngles.z);
}
//Between If it is on the smaller side of the wall, (Left side)
else if (startTower.transform.eulerAngles.y < Clamp0360(hit.transform.eulerAngles.y) || startTower.transform.eulerAngles.y > Clamp0360(hit.transform.eulerAngles.y-180))
{
//Between 300-360
if (startTower.transform.eulerAngles.y > Clamp0360(hit.transform.eulerAngles.y-180))
{
startTower.transform.eulerAngles = new Vector3(startTower.transform.eulerAngles.x, Mathf.Clamp(startTower.transform.eulerAngles.y,
Clamp0360(hit.transform.eulerAngles.y + 210), Clamp0360(hit.transform.eulerAngles.y + 270)), startTower.transform.eulerAngles.z);
}
//Between 0-90
else
{
startTower.transform.eulerAngles = new Vector3(startTower.transform.eulerAngles.x, Mathf.Clamp(startTower.transform.eulerAngles.y,
Clamp0360(hit.transform.eulerAngles.y- hit.transform.eulerAngles.y), Clamp0360(hit.transform.eulerAngles.y-30)), startTower.transform.eulerAngles.z);
}
}
}
Edit: Here is Clamp0360
public static float Clamp0360(float eulerAngles)
{
float result = eulerAngles - Mathf.CeilToInt(eulerAngles / 360f) * 360f;
if (result < 0)
{
result += 360f;
}
return result;
}
Any help would be greatly appreciated
Your answer
![](https://koobas.hobune.stream/wayback/20220612142100im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why is my object's local x rotation not going past 90 degrees? 1 Answer
Code to rotate around a local axis until local Y is 0? 0 Answers
Rotation jumping to a different rotation after rotating a certain amount 0 Answers
check x axis rotation,limit objects rotation 1 Answer
how to apply overall rotation to collection of rotated objects 0 Answers