- Home /
How can I get the rotation of Handles.DrawLine to match up with an Objects forward?
Hi all
I have this 2d field of view script for my ai which uses a Physics.OverlapCircle and then raycasts for obstacles to check if anything is blocking its view. This script is designed for enemies to look for the player. When the player is found a green ray is drawn between the two. The rays all start from the objects forward position.
function FindTargets(){
var targetInViewRadius = Physics2D.OverlapCircle(myPosition, losViewRadius,detectables);
if(targetInViewRadius){
targetTrans = targetInViewRadius.gameObject.transform;
targetPosition = Vector2(targetTrans.position.x , targetTrans.position.y);
var directionToTarget = (targetPosition - myPosition).normalized;
if (Vector2.Angle(transform.up, directionToTarget) < (losViewAngle /2) )
{
distanceToTarget = Vector2.Distance(myPosition, targetPosition);
if (!Physics2D.Linecast(myPosition, targetPosition, obstacles)) {
Debug.Log("Something detected");
Debug.DrawLine(myPosition, targetPosition, Color.green);
}
}
}
}
function DirectionForAngle(angleNumber : float, isGlobal : boolean){
if(!isGlobal){
angleNumber -= transform.eulerAngles.y;
}return Vector2(Mathf.Sin(angleNumber * Mathf.Deg2Rad),Mathf.Cos(angleNumber * Mathf.Deg2Rad));
}
I have a separate editor script which is designed to show the field of view cone in the scene view that looks like this. It uses editor handles and factors in the view angle to show the field of view.
#pragma strict
@CustomEditor(LineOfSight)
@CanEditMultipleObjects
class LookAtPointEditor extends Editor{
var viewAngleLeft : Vector2;
var viewAngleRight : Vector2;
function OnSceneGUI(){
var los : LineOfSight = target as LineOfSight;
Handles.color = Color.white;
Handles.DrawWireArc(los.transform.position ,Vector3.forward, Vector3.right, 360, los.losViewRadius);
viewAngleLeft = los.DirectionForAngle(-los.losViewAngle /2 , false);
viewAngleRight = los.DirectionForAngle(los.losViewAngle /2, false);
Handles.DrawLine (los.transform.position, los.transform.position + viewAngleLeft * los.losViewRadius);
Handles.DrawLine (los.transform.position, los.transform.position + viewAngleRight * los.losViewRadius);//DRAW ANGLE LINES
}
}
However if I rotate the enemy object although the actual field of view from the first script rotates the editor handles don't rotate with it and continue to stay in their place. How could I get the editor handles to rotate themselves along with enemy objects forward
Thanks
Your answer
Follow this Question
Related Questions
Problems with enemy ai 0 Answers
Raycasting not behaving as expected? 2 Answers
Unity Physics2D.Raycast always returns “the invoker” as hit 2 Answers
Unity 2D: Visualise a field of view cone 3 Answers
RaycastHit2D never contains a collider 2 Answers