Create Field of vision
Hello devs,
I'm trying to make a field of vision for a basic Artificial Intelligence. I'm just having trouble with the rotation part. I used "DrawWireDisc" to get a debug.
viewRadius=5;
Handles.DrawWireDisc(tr.position, tr.forward, viewRadius);
Vector3 angle1 = new Vector3(Mathf.Sin(viewRadius * Mathf.Deg2Rad), Mathf.Cos(viewRadius * Mathf.Deg2Rad), 0);
Vector3 angle2 = new Vector3(Mathf.Sin(viewRadius * Mathf.Deg2Rad), Mathf.Cos(viewRadius * Mathf.Deg2Rad), 0);
Handles.DrawLine(tr.position, tr.position + angle1 * viewRadius);
Handles.DrawLine(tr.position, tr.position + angle2 * viewRadius);
The problem is that when the angle is changed when I change the rotate.y instead of the rotate.y As the game is 2d I just do the rotation for y.
What am I doing wrong?
(In the images I change the rotation in y. And the opening of the angles does not move.)
Answer by ffabioteixeira · Mar 04, 2017 at 02:18 PM
Hello
Fix: https://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html
public Vector3 DirFromAngle(float angleInDegrees, bool angleIsGlobal)
{
Transform tr = GetComponent<Transform>();
angleInDegrees -= tr.eulerAngles.z;
return new Vector2(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), Mathf.Cos(angleInDegrees * Mathf.Deg2Rad));
}
Use function:
Vector2 viewAngleA = iconViewController.DirFromAngle(-iconViewController.viewAngle / 2, true);
Vector2 viewAngleB = iconViewController.DirFromAngle(iconViewController.viewAngle / 2, true);
Handles.DrawLine(tr.position, new Vector2(tr.position.x, tr.position.y) + viewAngleA * iconViewController.viewRadius);
Handles.DrawLine(tr.position, new Vector2(tr.position.x, tr.position.y) + viewAngleB * iconViewController.viewRadius);
Your answer
Follow this Question
Related Questions
Walking around a sprite 1 Answer
How to make the rotation of a child object of object A match the rotation of object B ? 0 Answers
[Solved] Problem with achieving rotation relative to current acceleration value 0 Answers
i'm using a code to make my cube walk with character controller but when i play the cube spins. 0 Answers
Unity 2D lookat and shoot issues 0 Answers