Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ThisIsMyAccount · Nov 16, 2016 at 07:27 AM · mesheditor-scriptingraycasthit2dfieldofviewstealth

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges