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 JackMini36 · Dec 19, 2014 at 04:09 PM · c#3dpositiondetection

Detect exactly where other objects around player are

I have a player (blue rectangle) which is the center object (can be moved). Other objects will move around the player. What I need is to detect exactly where the other objects lie from the perspective of the player, like the picture below. The circle represent the max distance/radius I need to detect other objects.

I can use InverseTransformPoint to find the x and y of other objects (I dont care about about z), What I need help with, is how can I know in which part (A,B,C..) they are, and how to set them up. Of course this must not be visible in-game and I prefer to handle it from code.

alt text

fig.jpg (55.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

3 Replies

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

Answer by jakovd · Dec 19, 2014 at 04:25 PM

Good approach would be to keep the references to all those objects in a script. Make a public List of Transforms in your script and drag'n'drop all other objects in the editor to that list. In your code, on the place where you need that information, make the foreach loop that goes through all the elements of that list and calculates the difference of their transform.position and Player.transform.position. Then get the angle of that vector and player's tranform.forward vector with Vector3.Angle(). Use if statements to differentiate which angle fits your A-H parts, like if (angle<15 && angle >-15) {//this is part C}.

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 JackMini36 · Dec 19, 2014 at 04:58 PM 0
Share

I tried what you suggested, but it is not working as expected

 foreach (GameObject car in cars)  
         {   
             if(car.transform.name == "$$anonymous$$ercedes_3")
             {
                 Vector3 centerPos = centerObject.transform.position; //player object
                 Vector3 carPos = car.transform.transform.position; // other object around player
                 float distance = Vector3.Distance(centerPos,carPos); // distance between the center object and the enemy car
                 
                 float angle = Vector3.Angle(carPos, car.transform.forward);
 
                 print (angle);
             }
         }

Both objects are cars. But the angle value does not change that much. I am always getting somewhere 160-170. I tried driving behind the other car, in front of it etc, but the angle change is $$anonymous$$imal.

avatar image jakovd · Dec 19, 2014 at 05:07 PM 0
Share

Distance (Line 7) is just a number (length of that distance). What you need is a vector pointing from centerPos to carPos. You get that by subtracting carPos-centerPos. Use that result to get the angle between carPos and that vector (Line 9).

avatar image JackMini36 · Dec 19, 2014 at 05:54 PM 0
Share

oh, I see, that was dumb! but now I have another problem. isn't the angle supposed to return positive and negative values depending on where the object is? I only get positive

avatar image tanoshimi · Dec 19, 2014 at 06:07 PM 0
Share

Vector3.Angle always returns the smallest angle between two vectors - it will always be between 0 - 180. If you want to have the "sign" of the angle, then you can use the cross product. Seeing as you're only working in 2D space, it will be something like:

 Vector2 fromVector = centerObject.transform.forward;
 Vector2 toVector = carPos-CenterPos;
 float ang = Vector2.Angle(fromVector, toVector);
 Vector3 cross = Vector3.Cross(fromVector, toVector);
 if (cross.z > 0) {
   ang = 360 - ang;
 }

 print(ang);


avatar image JackMini36 · Dec 19, 2014 at 06:52 PM 0
Share

@tanoshimi Tried it, but it returns only values between 180-360

Show more comments
avatar image
1

Answer by jeffreymarkbaldridge · Dec 19, 2014 at 08:11 PM

I find breaking these problems down into the most basic form to be easiest.
First put the "Other" objects into the player's space by subtracting the player's world position form the "other's" world position (we'll call this new Vector2, otherVec). This allows you to assume the players location to be the zero vector. Next, all you have to do is use Mathf.Atan(otherVec.x/otherVec.y) to find the degree in radians. Mathf has functionality to convert radians into degrees.
Then with the angle as jakovd stated, you can use if statements to translate into your A, B, C definitions.

http://mymathforum.com/algebra/6201-how-find-angle-given-point-circle.html

http://docs.unity3d.com/ScriptReference/Mathf.Atan.html

http://docs.unity3d.com/ScriptReference/Mathf.Rad2Deg.html

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
0

Answer by CodeTurtle · Dec 19, 2014 at 05:36 PM

You can calculate the vector between the player and the object, then decide which part the object it is at.

To explain this in a simpler way, imagine there's only four areas around the player(top-right, top-left, bottom-right, bottom-left). After calculated the vector between player and object:

new Vector2 (obj.x-player.x , obj.y-player.y)

We know that if the x and y values are greater than 0, then it is in the top-right section. If x is lower than 0 and y is greater than 0, then it is in the top-left, and so on.

If there are more sections, the same idea can still be applied.

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 jakovd · Dec 20, 2014 at 10:48 AM 0
Share

No hard feelings, but I think this solution is actually limited to four sections. Try thinking it through for eight and it soon gets too complicated.

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

29 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

Related Questions

Distribute terrain in zones 3 Answers

Position in relation to another object 1 Answer

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

Smooth movement between two exact locations 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