Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Sam Bigos · Apr 26, 2010 at 04:24 PM · raycastinganglevector

Check if player is in front of enemy?

I have a script that checks whether the player is in line of sight of an enemy using raycasting, however I always want to only return true if the player is in front of the enemy. How would I do that.

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

4 Replies

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

Answer by Eric5h5 · Apr 26, 2010 at 04:39 PM

See this answer, except use Z instead of X.

Comment
Add comment · Show 4 · 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 StephanK · Apr 26, 2010 at 04:43 PM 0
Share

That's even easier... ;)

avatar image DESTRUKTORR · Aug 01, 2012 at 01:58 AM 0
Share

That resource is no longer available. Would you $$anonymous$$d directing me to an updated location?

avatar image Eric5h5 · Aug 01, 2012 at 02:19 AM 0
Share

Updated link.

avatar image Judgeking · May 11 at 08:23 AM 0
Share

code from link:

 var otherTransform : Transform;
 ...
 ...
 function Start () {
     var relativePoint = transform.InverseTransformPoint(otherTransform.position);
     if (relativePoint.x < 0.0)
         print ("Object is to the left");
     else if (relativePoint.x > 0.0)
         print ("Object is to the right");
     else
         print ("Object is directly ahead");
 }
avatar image
4

Answer by StephanK · Apr 26, 2010 at 04:42 PM

Calculate the angel between your forward vector and the enemy position. If absolute value is bigger than a certain threshold (eg. 90 degrees) it's behind you. To do that there a some very convenient functions:

Vector3 directionToTarget = transform.position - enemy.position;
float angel = Vector3.Angel(transform.forward, directionToTarget);
if (Mathf.Abs(angel) > 90)
    Debug.Log("target is behind me");

Not tested but should work.

Comment
Add comment · Show 6 · 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 Eric5h5 · Apr 26, 2010 at 11:13 PM 2
Share

I totally wish there was a Vector3.Angel function. :)

avatar image StephanK · Apr 27, 2010 at 05:56 AM 0
Share

Well, sometimes even wishes come true... http://unity3d.com/support/documentation/ScriptReference/Vector3.Angle.html

avatar image Eric5h5 · May 30, 2010 at 04:00 AM 1
Share

@spree: a month late, but: no, that did not make my wish come true. Read again, more carefully. ;)

avatar image StephanK · May 31, 2010 at 02:27 PM 0
Share

Lol. I see it now... ;)

avatar image mrCrush · Oct 19, 2017 at 08:18 PM 0
Share

Thank you! It helped a lot!

Show more comments
avatar image
1

Answer by asimov · Jun 25, 2012 at 02:54 PM

I know it's been a month since any comments were left on this, but posting as the link in the answer no longer works. So regarding spree 1's code above, for complete 360 degree detection, I think it should be;

     if (Mathf.Abs(angel) > 90 && Mathf.Abs(angel) < 270)
         Debug.Log("target is in front of me"); 

     if (Mathf.Abs(angel) < 90 || Mathf.Abs(angel) > 270)
         Debug.Log("target is behind me");   
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 CollosalChris · Aug 06, 2017 at 06:03 AM

Check out InverseTransformPoint as mentioned, but placing code here for better reference:

 public Transform cam;
 public Vector3 cameraRelative;

 void Start()
 {
     cam = Camera.main.transform;
     Vector3 cameraRelative = cam.InverseTransformPoint(transform.position);

     if (cameraRelative.z > 0)
         print("The object is in front of the camera");
     else
         print("The object is behind the camera");
 }


Comment
Add comment · Show 1 · 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 florinel2102 · Jun 21, 2020 at 03:11 PM 0
Share

For people who doesn't work as expected , try to adjust (ins$$anonymous$$d of cameraRelative.z > 0 to do something like this cameraRelative.z > 0.15f ) .

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

7 People are following this question.

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

Related Questions

Vector to angle 3 Answers

Arbitrary rotation around a point works except when up flips? 1 Answer

Facing angle of an object 1 Answer

Raycast in the direction of looking plus an angle offset 0 Answers

Raycasting in the direction of a target but with a fixed angle to the ground 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