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 /
This question was closed Feb 25, 2015 at 09:09 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Fraek · Feb 24, 2015 at 01:28 PM · 2drotationrigidbody2dvelocity2d-physics

Movement Relative to Rotated Parent

Greetings,

I'm trying to create a child-object (rigidbody2D) inside another rigidbody2D. I can move them seperately which works fine. The parent object is rotating when pressing left and right, the child object is moving directly over the X and Y axis (so left and right, no rotation involved).

The big problem is that when parent objects are rotated, the child objects are still moving in straight lines X and Y. So relative to the parent they are moving in wrong directions.

 public void moveRight()
     {
         rigidbody2D.velocity =  new Vector2(1, 0);
     }
     public void moveLeft()
     {
         rigidbody2D.velocity = new Vector2(-1, 0);
     }
     public void moveUp()
     {
         rigidbody2D.velocity = new Vector2(0, 1);
     }
     public void moveDown()
     {
         rigidbody2D.velocity = new Vector2(0, -1);
     }
     public void moveStop()
     {
         rigidbody2D.velocity = new Vector2(0, 0);
     }

The problem is that the velocity is set not taking into account the parent object.

 rotationInDegrees = Mathf.Atan2(attachedTo.rigidbody2D.position.y,
 attachedTo.rigidbody2D.position.x) * Mathf.Rad2Deg;

I can use this with Cos and Sin. But I never can get it quite right.

Instead of directly setting the velocity I found/came up with the following code, but again it is not taking into account the parent's rotation.

 void FixedUpdate(){
     attachedTo.rigidbody2D.position.x) * Mathf.Rad2Deg;
     rigidbody2D.velocity = new Vector2(Mathf.Lerp(0, Input.GetAxis("Horizontal") * 5, 0.8f),
                                     Mathf.Lerp(0, Input.GetAxis("Vertical") * 5, 0.8f)); 

I'm currently lost as I want the character to move by the last piece of code but also take into consideration the parent's angle to move in the right direction... Any piece of advice on how to do this? Or possibly an answer?! Ive been looking but haven't found an answer on the interwebs... Sidequestion: can I force the rigidbody2D to stay inside the parent?

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
0
Best Answer

Answer by gfoot · Feb 24, 2015 at 08:09 PM

Physics is not performed relative to the parent - it all happens externally, then the world-space values for rigid body positions get put back into the objects.

One option you have is to use constraints to constrain the child to the parent's axis. This will give a physically plausible result when the parent rotates while the child is already moving. Look them up if it sounds useful.

You might also want to apply your forces relative to the parent, so instead of using Vector2(1, 0) you can transform it using the parent's transform, to get a new world-space vector that's not just along the world X axis.

 Vector2 worldVector = parent.transform.TransformVector(localVector);

It really depends what you're trying to do, and actually I find myself doubting that you really want physics involved here at all. You may be better off just not using rigidbody2d at all.

Comment
Add comment · Show 4 · 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 Fraek · Feb 24, 2015 at 10:06 PM 0
Share

$$anonymous$$aking the child restrict to parent's axis was already happening. This indeed makes the child-object stick to the parent when controlling the parent. When controlling the child-object and adjusting velocity of the rigidbody2d it is also moving relative from the parent-object when you make it Extrapolate.

However it is not taking into account the rotation of the parent's object when controlling the child object. I have tried out multiplaying by the parent's transform but what part of the transform you want me to use. The position, scale, rotation? As I'm not sure how to use the transform.rotation as an object to multiply the velocity by.

I've tried something like below but it's giving me far from any good results.

 rotation = $$anonymous$$athf.Atan2(attachedTo.rigidbody2D.position.y, attachedTo.rigidbody2D.position.x);
 
 rigidbody2D.velocity = 
      Vector2.Scale(   new Vector2($$anonymous$$athf.Lerp(0, Input.GetAxis("Horizontal") * 5, 0.8f), $$anonymous$$athf.Lerp(0, Input.GetAxis("Vertical") * 5, 0.8f)) ,
                       new Vector2($$anonymous$$athf.Cos(rotation), $$anonymous$$athf.Sin(rotation)) );

avatar image gfoot · Feb 24, 2015 at 10:41 PM 1
Share

Use attachedTo.transform.TransformVector(localVector) to get a world-space version of a vector relative to attachedTo.

avatar image Fraek · Feb 25, 2015 at 08:53 PM 1
Share

You sir, amazing! For anyone else wondering what the final solution was for the problem below is the code.

 void FixedUpdate(){
         rigidbody2D.velocity = attachedTo.transform.TransformVector(    new Vector2($$anonymous$$athf.Lerp(0, Input.GetAxis("Horizontal") * 5, 0.8f),
                                                                         $$anonymous$$athf.Lerp(0, Input.GetAxis("Vertical") * 5, 0.8f))
                                                                    );
     }
avatar image Fraek · Feb 25, 2015 at 09:09 PM 0
Share

Answer was given by gfoot:

"Use attachedTo.transform.TransformVector(localVector) to get a world-space version of a vector relative to attachedTo."

     void FixedUpdate(){
         rigidbody2D.velocity = attachedTo.transform.TransformVector(    new Vector2($$anonymous$$athf.Lerp(0, Input.GetAxis("Horizontal") * 5, 0.8f),
                                                                         $$anonymous$$athf.Lerp(0, Input.GetAxis("Vertical") * 5, 0.8f))
                                                                    );
     }
 

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Unity C# 2D Adding Velocity on rotation 1 Answer

Projetil rotation in straight line trajectory 2 Answers

How to rotate rigidbody2d's sprite when colliding?,How to rotate sprite of rigidbody2d, but without friction? 0 Answers

2D Geometry dash-like ship physics 0 Answers

Rigidbody2D.velocity.x not working but Y does 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