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 /
  • Help Room /
avatar image
0
Question by Rangr · Jan 30, 2016 at 01:17 AM · c#distanceanglelinecast

Angles and Linecasting

I'm working on a script to control guards in a stealth game. I think I'm misusing the angle command or physics.linecast in some way, because it's not working. I want to get the angle between the guard and the player relative to which way the guard is facing so that if the player is within 40 or so degrees of the center of the guards vision they are considered seen and if they're in the next 20 degrees they are considered in the peripheral vision and the guard will turn to the player until the player is seen. My code for this segment is below along with a picture hopefully illustrating what I'm trying to do. If anyone could explain my error in this, that would be excellent. Thanks!

                 distance = (transform.position - target.position).magnitude;
         if(distance < 300f)
         {
             Vector3 angle = (transform.position - target.position).normalized;
             float rangle = Vector3.Angle(transform.forward, angle);
             if(rangle < 120 && rangle > 85) //periferal vision
             {
                 //raycast, if uninterupted, check movement, if moving, turn to get player in normal vision
                 int layerMask = 1 << 2;
                 RaycastHit hit;
                 if (Physics.Linecast(transform.position, target.position, layerMask) == false)
                 {
                     Debug.Log("In Perifery");
                     if(target.GetComponent<CharacterController>().GetMovement() != Vector3.zero)
                     {
                         Debug.Log("In Perifery moving");
                     }
                 }
             }
             else if (rangle < 85) //normal vision
             {
                 //raycast, if uninterupted, player is seen
                 int layerMask = 1 << 2;
                 if (Physics.Linecast(transform.position, target.position, layerMask) == false)
                 {
                     contact = true;
                     Debug.Log("In Vision");
                 }
             }
         }

alt text

ex1.png (24.0 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Orami · Jan 30, 2016 at 01:41 AM

I have some code that does that in a 3d world... maybe you can adapt it? This uses a cone from the eye.

I would run and see if it is in the greater arc then the minor arc so you don't do double work when you don't need to.

 float CheckSightArc(Vector3 npc, Vector3 pc)
     {
         float Angle = Vector3.Angle (pc - npc, transform.forward);
         return Angle;
     }

 bool SeePlayer()
     {
         Vector3 npcPos = new Vector3 ();
         npcPos = transform.position;
         Vector3 playerPos = FP.GetComponent<Transform> ().position;
         npcPos.y += raiseTransformBy;
         Ray visRay = new Ray(npcPos, playerPos - npcPos);
         RaycastHit playerHit;
         Debug.DrawRay (npcPos, playerPos - npcPos, Color.green);
         if (Mathf.Abs(CheckSightArc(npcPos, playerPos)) < Sight_Arc && Physics.Raycast (visRay, out playerHit, Sight_Range)) 
         {
             if(playerHit.collider.tag == "Player")
             {
                 EngagePlayer();
                 return true;
             }else
             {
                 if(debug_toggle)Debug.Log ("My view is blocked by something with the tag of: " + playerHit.collider.tag);
             }
         }
         return false;
     }
Comment
Add comment · Share
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
avatar image
1

Answer by toddisarockstar · Jan 30, 2016 at 01:51 AM

why not use LookRotation. then simply subtract the new rotation from your characters current rotation to see if you are less than 20?

  var looking = Quaternion.LookRotation (enemy.position - transform.position);
 
 
Comment
Add comment · Share
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

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

68 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

raycast : get normal value of linecast hit 1 Answer

How I can make my camera keep a distance between two objects? 0 Answers

Move a game object away from another game object based on the angle difference 1 Answer

Modify Draw Distance via Script? 1 Answer

Set vehicle driver position (calculate position with angle and distance) 1 Answer


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