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 · Dec 11, 2016 at 11:20 PM · 2draycasteditor-scriptingphysics2dfieldofview

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

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


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