- Home /
2-in-1 question. How do I get this script made mesh and Editor UI Handles to face the same direction as my field-of-view cone?
Hey everyone
I've been following this tutorial from Youtube and have been trying to convert it to 2D over the last few days. The idea behind the script is that it finds collisions in certain obstaclemask at a set view angle (which is shot from the transform.up point), then raycasts to see if there's anything blocking its sight. I'm working on a 2d stealth game so in the final game this would be used to trigger an alert sequence. As well this it constructs a mesh to show the actual field of view in game and in a seperate script shows the field of view outlines in the scene view.Most of it works, however this is where the problem is. If I rotate the object the script is attached to the field-of-view rotates but the mesh and editor outlines don't move along with it. The script here is the field-of-view mesh
function Start(){
fovMesh = Mesh();
fovMeshFilter.mesh = fovMesh;
}
function Update(){
myPosition = Vector2(transform.position.x,transform.position.y);
FindTargets();
}
function LateUpdate(){
DrawFieldOfViewMesh();
}
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));
}
function DrawFieldOfViewMesh(){
var stepCount : int = Mathf.RoundToInt(losViewAngle * meshResolution);
var stepAngleSize : float = losViewAngle / stepCount;
var viewPoints = List.<Vector2> ();
var oldViewCast : ViewCastInfo = new ViewCastInfo();
for(var i : int = 0;i<= stepCount; i++){
var angle : float = transform.eulerAngles.y - losViewAngle / 2 + stepAngleSize * i;
Debug.DrawLine(transform.position, transform.position + DirectionForAngle(angle,true) * losViewRadius, Color.red);
var newViewCast : ViewCastInfo = ViewCast(angle);
viewPoints.Add(newViewCast.point);
oldViewCast = newViewCast;
}
var vertexCount : int = viewPoints.Count + 1;
var vertices : Vector3[] = new Vector3[vertexCount];
var triangles = new int[((vertexCount-2) *3)];
vertices[0] = Vector3.zero;
for( i = 0;i<vertexCount-1;i++){
vertices[i+1] = transform.InverseTransformPoint(viewPoints[i]);
if(i<vertexCount-2){
triangles[i*3] = 0;
triangles[i*3+1] = i+1;
triangles[i*3+2] = i+2;
}
}
fovMesh.Clear();
fovMesh.vertices = vertices;
fovMesh.triangles = triangles;
fovMesh.RecalculateNormals();
//fovMesh.RecalculateBounds();
}
function ViewCast(globalAngle : float) : ViewCastInfo{//gets points to construct mesh with
var dir = DirectionForAngle(globalAngle , true);
var hit = Physics2D.Raycast(myPosition,dir, losViewRadius, obstacles);
if(hit){
return ViewCastInfo(true,new Vector2(hit.point.x,hit.point.y),hit.distance,globalAngle);
}else{
return ViewCastInfo(false,myPosition + dir * losViewRadius,losViewRadius,globalAngle);
}
}
class ViewCastInfo extends System.ValueType{
public var hit : boolean;
public var point : Vector2;
public var dst : float;
public var angle : float;
public function ViewCastInfo(_hit : boolean,_point : Vector2, _dst : float, _angle : float)
{
hit = _hit;
point = _point;
dst = _dst;
angle = _angle;
}
}
I've only included the important parts and cut a bit out as it's a pretty big script. The script here is the custom editor script
#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
}
Could someone help me figure out how to get the custom mesh and editor handles to move along with the actual field of view area itself?
Thanks
Your answer
Follow this Question
Related Questions
new Mesh() works outside of array, but not in array. Huh? 3 Answers
How can I get the rotation of Handles.DrawLine to match up with an Objects forward? 0 Answers
Unable to save the mesh when RecalculateNormals() is applied to the mesh in the editor script 1 Answer
Find number of Meshes from FBX in Editor Script 0 Answers
Enemy Line of Sight Help Needed 2 Answers