Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 jakejolli · Jan 25, 2015 at 03:33 PM · c#3dposition2d spritesrelative

Position in relation to another object

My game uses 2D enemies in 3D space. The enemies graphics are obviously flat and are always facing the player. However, I want a different graphic to be displayed for each enemy based on a variable facingDirection which has an enum value corresponding to 8 compass points (N, NE, E, SE, S, etc.) as well as the player's position in relation to the enemy.

To be more clear: If the enemy is facing north, and the player is standing to the north of the enemy, I want the enemy's graphic to display its front. If the enemy is facing north and the player is standing to the south of the enemy, I want the player to see its back.

I have an algorithm to calculate this, but it's somewhat complex and I don't want to waste time implementing it if there is a built-in function within Unity.

My question is: Is there such a function in Unity (To find one gameobject's position in relation to another)?

Any help is appreciated.

Comment
Add comment · Show 4
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 Simon-Larsen · Jan 25, 2015 at 03:48 PM 0
Share

$$anonymous$$aybe using an animator and changing its states would suffice?

avatar image jakejolli · Jan 25, 2015 at 03:59 PM 0
Share

Yes. But I would still need to know the player's position in relation to the enemy in order to deter$$anonymous$$e which state to use.

avatar image jakejolli · Jan 25, 2015 at 09:53 PM 0
Share

Perhaps I misunderstand, but this sounds like a simple Vector3 subtraction operation to me.

You can "Normalize" the result to get a vector of magntiude 1, a "direction".

if you want an angle as the result (the Atan2 operation does NOT need normalization)...

Subtraction was part of my algorithm, but I was working with x and y individually. Didn't realize there was an operator for subtracting vectors. I think I can make use of Atan2 as well. If you want to convert this to an answer, I would be happy to accept it.

Thanks, Jake

avatar image Glurth · Jan 25, 2015 at 10:29 PM 0
Share

You can perform $$anonymous$$ANY operations with vectors: you can add & subtract two vectors, you can multiply or divide (scale) a vector by a float, and then there are the CrossProduct and DotProducts which while not technically "multiply" operations, are kind of like two different ways of multiplying two vectors (but wiki them for details).

1 Reply

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

Answer by Glurth · Jan 25, 2015 at 04:51 PM

To find one gameobject's position in relation to another

Perhaps I misunderstand, but this sounds like a simple Vector3 subtraction operation to me.

 Vector3 player_relative_to_enemy=enemy.transform.position - player.transform.position;

You can "Normalize" the result to get a vector of magntiude 1, a "direction".

 player_relative_to_enemy.Normalize();

if you want an angle as the result (the Atan2 operation does NOT need normalization)...

 float angle=Mathf.Atan2(player_relative_to_enemy.x,player_relative_to_enemy.y);

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Detect exactly where other objects around player are 3 Answers

Distribute terrain in zones 3 Answers

How to set to game objects's position from 2 different game objects arrays equal to each other? 0 Answers

Why is the camera going crazy when the player moves? 0 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