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 Sartoris · Oct 23, 2020 at 05:28 PM · rotationpositionangle

How to get 0-360 bearing in 2D?

Hi everyone,

in my top-down 2D project I have a submarine that can rotate and move around the map. I would like to find the bearing to another 2D object, such as a ship. The 0 bearing is to the front of the submarine (its bow, or "nose"), the 90 bearing is to its right, 270 is to its left, and 180 is directly behind it.

alt text

The code I pasted below sort of works: it returns a bearing that is close to what it should be, BUT only when the player submarine is stationary. As soon as it starts to move, the values do not change as they should. Essentially, this is an example of the value that should be returned after the submarine has rotated:

alt text

I have tried using this approach: first I determine if the object is to the left or right of the submarine, then I find the angle, and finally show that angle in my game. However, this isn't working, i.e. the wrong bearing is shown.

I'd appreciate any help or ideas on how to approach this problem.

Here's the code that I tried to refactor for 2D from this link: link text

 int AngleDir(Vector3 forwardVector, Vector3 targetDirection, Vector3 upVector) {
 
 float direction = Vector3.Dot(Vector3.Cross(forwardVector, argetDirection), upVector);
 
 if (direction > 0f) {
        return 1;
 } else if (direction < 0f) {
        return -1;
 } else {
        return 0;
    }
 }
 
 
 // Takes a starting object's Transform, and a target object's position, and returns the angle to the target 0-359° 
 public float GetBearing(Transform startTransform, Vector3 targetPosition) {
 
 Vector3 vectorToTarget = targetPosition - startTransform.position;
 
 float angleToTarget = Vector3.Angle(startTransform.right, vectorToTarget);
 
 int direction = AngleDir(startTransform.right, vectorToTarget, startTransform.up);
 
 return (direction == 1) ? 360f - angleToTarget : angleToTarget;
 }


snimka-zaslona-2020-10-24-102427.png (15.7 kB)
snimka-zaslona-2020-10-24-083808.png (10.9 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

Answer by Zimbwawa · Oct 23, 2020 at 11:29 PM

What does angledir do? And try splitting it into multiple lines, it looks nicer when its compact but it should be much easier to read and debug if it's spread over multiple lines. You may also wan't to look at Transform.InverseTransformPoint, it transforms a point from world space to local space. It might make it a bit easier to calculate the relative angle if you do that. https://docs.unity3d.com/ScriptReference/Transform.InverseTransformPoint.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 Klarzahs · Oct 23, 2020 at 11:40 PM

Hi @Sartoris,

If you are using a topdown 2d view, are you by chance in a 3d view setting? Then you x/y coordinates in ScreenSpace are actually x/z in world space.
Your code then becomes

 float angle = Mathf.Abs(Mathf.Round(Mathf.Atan2(target.position.z - player.position.z, target.position.x - player.position.x) * Mathf.Rad2Deg));

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 Sartoris · Oct 24, 2020 at 07:11 AM

@Zimbwawa Hey Zimbwawa, sorry about the messy outline. I just reworked the post to make it read more easily, I hope that it's more understandable now. Since I posted this question I found a link here on the forum that offers one solution, but in 3D. I tried reworking it for 2D, and I posted that code instead. However, I'm not sure if I'm doing it right, as the values that are returned are not correct. Thank you for your suggestion about InverseTransformPoint, I'll see if I can apply that.


@Klarzahs Hey Klarzahs, thanks for answering, but I'm working in the 2D camera space environment, and everything else is working in that regard, so I don't think the solution is in exchanging z for y.

In the meantime I've changed the code to this:

 Vector3 direction = target.position - player.position;
 
 float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
 
 float bearing = (angle + 360) % 360;    
 

And it works, but the bearing does not change when the sub rotates. Instead, the program thinks that the sub is constantly pointing to the right, and reports the bearing based on that assumption. I would need a way to let the code know that it needs to take the rotation into consideration.

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

209 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get offset postition after rotation ? (Spent hours figuring it out) 3 Answers

Position from rotation problem. 1 Answer

How to use the lerp function with rotation? (C#) 1 Answer

how to ignore transform.position.y 3 Answers

How can I limit the rotational speed of a gameObject tracking my mouse? 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