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 /
This question was closed Jan 05, 2020 at 05:57 AM by umiata for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by umiata · Jan 04, 2020 at 12:35 AM · rigidbody2dvector2localspaceglobalspace

How does GetRelativeVector work?

I found some code online for a simple 2D car movement script. However, I want to understand WHY it works the way it does so I can make tweaks to it. I'm particularly confused with 'GetRelativeVector':

 float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up));
          if(direction >= 0.0f) {
              rb.rotation += h * steering * (rb.velocity.magnitude / 5.0f);
              //rb.AddTorque((h * steering) * (rb.velocity.magnitude / 10.0f));
          } else {
              rb.rotation -= h * steering * (rb.velocity.magnitude / 5.0f);
              //rb.AddTorque((-h * steering) * (rb.velocity.magnitude / 10.0f));

(where 'rb' is an instance of Rigidbody2D). Here's what the manual says: alt text As far as I can tell, Vector2.up is just synonymous to a simple vector (0,1). And this vector is the 'local space vector' argument. So what would rb.GetRelativeVector(Vector2.up) be?

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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Owen-Reynolds · Jan 04, 2020 at 01:35 AM

That exact equation rb.GetRelativeVector(Vector2.up) looks like it's simply getting the [EDITS: xxforwardxx -- opps, UP] unit vector. [xxIt turns out there's no easier way to get thatxx]. rb.rotation is a special 0-360 float. It seems to work independently of rb.transform. An easy shortcut to spin rigibbodies (I don't know 2D -- not sure why regular 2D sprites have to use a 3D rotation).


All rb.GetRelativeVector does is spin the input by -rotation degrees. If rotation is 90, it turns (5,0) into (0,5).

Comment
Add comment · Show 2 · 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 umiata · Jan 04, 2020 at 05:05 AM 0
Share

Ok, gotcha! I'm not sure why this was giving me so much trouble. Another question, though: would I be able to replace rb.GetRelativeVector(Vector2.up) with just transform.up? What is the difference between the 'transform' and the Rigidbody2D variable 'rb'? Aren't they one and the same?

avatar image Owen-Reynolds · Jan 04, 2020 at 03:56 PM 0
Share

I think yes: rb.transform.up is the same as that longer way, sort of. It's a Vector3 with z always 0, which means it acts like a Vector2 (assigning v2=rb.transform.up is safe).

There's one tiny issue: if you manually change rb.rotation (ins$$anonymous$$d of letting physics do it), there's a 1-frame lag until transform.rotation.z catches up (which is very weird). So if other parts of code may change it, the long way is probably safer to exa$$anonymous$$e the actual up vector.

Follow this Question

Answers Answers and Comments

119 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

Related Questions

Rb2D.MovePosition not working - weird error... 1 Answer

Push an Object with a vector created from two points. 0 Answers

CharacterControllers and Transform.Rotate 1 Answer

Raycast 2D 0 Answers

Character won't move (Fixed) 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