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 Xantec · Jul 30, 2014 at 11:27 PM · rigidbodyvelocitydirection

NSEW Direction of Rigidbody Velocity

I need to get the North, South, East or West Direction of a rigidbody's movement. I can have North-East, or South-West. Only North, South, East or West.

Here is what I've tried so far, but to no avail :

 int GetDirection()
     {
         int direction = previousDirection;
         float angle = Vector3.Angle(Vector3.up, rigidbody.velocity);
         angle = Vector3.Dot(Vector3.right, rigidbody.velocity.) > 0.0 ? angle : -angle;
         Debug.Log(angle);
         if(angle > 45 && angle <= 135)
         {
             direction = 1;
         }
         if ((angle > 135 && angle <= 180) || (angle < 0 && angle >= -45))
         {
             direction = 2;
         }
         if (angle < -45 && angle > -135)
         {
             direction = 3;
         }
         if ((angle < -135 && angle > -180) || (angle > 0 && angle < 45))
         {
             direction = 0;
         }
         direction = 0;
         return direction;
     }

 

0 being north 1 being east 2 being south 3 being west

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
1

Answer by rutter · Jul 30, 2014 at 11:36 PM

Seems to me that it would be easier to achieve with two dot products:

 Vector3 direction = rigidbody.velocity.normalized;
 float dotEast = Vector3.Dot(Vector3.right, direction);
 float dotNorth = Vector3.Dot(Vector3.forward, direction);

The above assumes that Vector3.right and Vector3.forward are east and north. Any pair of perpendicular unit vectors will work.

If dotEast is positive, the direction is pointing "more east".

If dorNorth is positive, the direction is pointing "more north".

You could compare the absolute value of the two to see which is more important:

 if (Mathf.Abs(dotEast) > Mathf.Abs(dotNorth)) {
     //heading east/west
 } else {
     //heading north/south

     //TODO: what if the values are equal?
     //as written, this code assumes north/south
 }
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 Owen-Reynolds · Jul 30, 2014 at 11:42 PM

Angle from UP (what you're starting with) isn't helpful. It doesn't give the angle around up -- it tells you the angle to get to the up vector. Suppose the angle with UP is 90. That means the object is moving perfectly flat, but in any direction.

Using angle from forward would work better. Your dot-product test fixes the "can't tell left from right" problem. But still a small problem. Suppose the object is moving mostly up, and a little north-ish. The angle will be 80 degree+, making you think you're going south.

One fix is to use velocity without the y: Vector3 v = rigidbody.velocity; v.y=0; then use v, to get a "flat" angle from forward.

But, it seems easier to directly compare the values. If you're going West, then the velocity will have -x as the largest. So:

 Vector vv = rigidbody.velocity;
 if(Mathf.Abs(vv.x)>Mathf.Abs(vv.z)) {
   // x wins: North or south
   if(vv.x>0) dir=N; else dir=S;
 }
 else {
   // east or west:
   if(vv.z>0) dir=E; else dir=W;
 }
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 Xantec · Jul 31, 2014 at 05:05 AM

Here's the solution I made right before leaving for work. int GetCompassDirection() { int direction = previousDirection; //Debug.Log(rigidbody.velocity.normalized); float normX = rigidbody.velocity.normalized.x; float normY = rigidbody.velocity.normalized.y;

         if(Mathf.Abs(rigidbody.velocity.x) > Mathf.Abs(rigidbody.velocity.y))
         {
             if(normX >= 0.1)
             {
                 //East
                 direction = 1;
             }
             else if (normX <= -0.1)
             {
                 //West
                 direction = 3;
             }
         }
         else if (Mathf.Abs(rigidbody.velocity.x) < Mathf.Abs(rigidbody.velocity.y))
         {
             if(normY >= 0.1)
             {
                 //North
                 direction = 0;
             }
             if (normY <= -0.1)
             {
                 //South
                 direction = 2;
             }
         }
         previousDirection = direction;
         return direction;
     }
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

Change velocity direction 1 Answer

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

Rigid Body relative speed 1 Answer

How can I convert velocity/direction to Force? 3 Answers

Throwing knife goes straight 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