- Home /
Fade color depending on Angle (angle 360° instead of 180°)
Greetings people.
First of all, I want to warn you that I’m a noob and just starting out with Unity and scripting. So thank you in advance for taking time to read this.
I have different points of information on an object marked by World Space GUI buttons and dots (images).
Here is an explanatory photo.
I have an Orbit camera that rotates around the object and can zoom in and out.
What I want to achieve is that when I rotate towards the back of the object, the Button and dots on the back have a stronger color (alpha “color” is higher) while the two buttons in the front fade to 30% of their alpha “strength”.
I don’t want the zoom function to influence the fade, that’s why I thought it would be more convenient to work with Angle instead of Distance. But I only want to take into account the angle while I rotate in the Y-axis (from back to front ◄-►). I want to ignore the angle when I rotate Up-Down ▲-▼.
I used Vector3.Angle, but I can’t make it ignore the Up-Down rotation and my second problem is that with Vector3.Angle, the back and the front are both 90°. I need to differentiate back and front, so I would need a number from 0° to 360° or -180° to 180°.
I always calculate the Angle between the camera and the center where it is aiming at. I read in another answer that I should use Vector3.Dot, Vector3.Cross and Mathf.Sign. I tried to, but since I’m a noob the results weren’t as expected. I’ll be very thankful for any help or guidance in the right direction.
Answer by WillNode · May 14, 2015 at 01:03 PM
unity's Vector3.Angle is always returning the nearest angle between two vectors, here's how I did to know the angle on clockwise direction (Y-Axis)
float angle(Vector3 from, Vector3 to)
{
//Set 'to' vector so 'from' always Vector3.forward
Quaternion q = Quaternion.FromToRotation(from, to);
//Get rotation, the resulting value are between -180 to 180
return angleY(q*Vector3.forward);
}
float angleY(Vector3 v)
{
if (v.z > 0)
return Vector3.Angle (Vector3.forward, v);
else
return -Vector3.Angle (Vector3.forward, v);
}
please note that to make this works, the Y value on both vector are always zero
Your answer
Follow this Question
Related Questions
Debug.Log(Vector3.Angle(ray.direction,hit.normal)); HELP! 1 Answer
Moving On Two Axes and Reading a Stable Angle 0 Answers
Forward angles 2 Answers
Get angle beetween surfaces 1 Answer