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
12
Question by Henk Jan Baard · Mar 13, 2010 at 01:56 AM · gameobjectdirection

How to determine if enemy is on my left or right hand side?

I have 2 game objects. Game Object A and Game Object B. GO A is facing a direction and I want to know if GO B is on the left or right of GO A.

To illustrate my problem I made a picture:

Facing example

The red line is the direction I'm facing (the z axis of GO A is pointing that way).

So how can I determine if GO B is facing to the left or right as seen from GO A, regardless of their positions in world space?

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
34
Best Answer

Answer by Eric5h5 · Mar 13, 2010 at 02:56 AM

Use InverseTransformPoint:

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"); }

Comment
Add comment · Show 8 · 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 Lipis · Mar 13, 2010 at 03:22 AM 0
Share

@Eric5h5 you might would like to start using brackts {} all the time after watching that video.. http://www.youtube.com/watch?v=hQVTIJBZook Strongly recommended if you are dealing with JavaScript!

avatar image Eric5h5 · Mar 13, 2010 at 03:28 AM 0
Share

I do use brackets all the time in my own code (they are put in automatically anyway when writing if statements etc.). When writing forum code I don't.

avatar image Lipis · Mar 13, 2010 at 03:42 AM 2
Share

But when writing forum code it's usually for beginners so I think it's better to include them for best practice. Watch the video anyway.. it's really worth it!

avatar image Henk Jan Baard · Mar 13, 2010 at 12:33 PM 5
Share

A beginner in Unity doesn't always mean a beginner in program$$anonymous$$g :-)

avatar image Joshua · Apr 12, 2011 at 04:35 PM 1
Share

Old answer but still helped me, was thinking of complicated linear algebraic ways to calculate something that unity has a simple function for.. Thanks! ^.^

Show more comments
avatar image
0

Answer by Jason_DB · Mar 13, 2010 at 02:18 AM

You could make two raycasts, one(1) from A directly forward and one(2) from A to B, and then compare the x components of their directions (I think that it would be left if 2.x < 1.x and right if 2.x > 1.x).

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 edmundo096 · Aug 28, 2013 at 11:01 PM 0
Share

I was also thinking about using Raycasts but using InverseTransformPoint seems less complicated and has better performance.

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to point the mission direction using an arrow? 2 Answers

melee system script problem, what did i do wrong or what to do next? 0 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

C# Adding Components From Other Gameobjects 3 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