• 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 mangosauce_ · Mar 21, 2021 at 10:25 PM · movementphysicsrigidbodyrigidbody2dphysics2d

Child GameObject can't use MovePosition on parent's Rigidbody2D?

Howdy! I'm attempting to use a script on a child gameObject to call MovePosition on its parent's Rigidbody. This, surprisingly, does not work in the least. Previously I had success calling Rigidbody.MovePosition in the parent script, attached to the same gameObject as the Rigidbody. However, as I'm trying to compartmentalize my code into specialized component scripts located in a child object, I moved this code to a child script.


Is this a limitation with Unity?


So far I have tried:

  1. Using MovePosition (Didn't work.)

  2. Setting the Rigidbody's velocity (Didn't work.)

  3. Using AddForce (ForceMode2D Impulse & Force) (Didn't work.)

  4. Calling a method in the parent script (from the child) to move the Rigidbody based on the child's Velocity property. (Didn't work.)

  5. Merging the movement code into the parent script again. (Works, but doesn't follow the rules I've established for my scripts and bloats the parent script substantially.)


    Movement script in child gameObject:

             // Actuate movement based on the script's Velocity property.
             private void FixedUpdate()
             {
                 // The Rigidbody property returns the parent's Rigidbody2D
                 // successfully, and the following code executes without a hitch:
                 if (Rigidbody)
                 {
                     if (Velocity.magnitude > 0)
                         ApplyVelocity();
     #if UNITY_EDITOR
                     // This debug variable affirms that the Velocity is set
                     //  appropriately (it is).
                     debugSpeed = Velocity.magnitude;
     #endif
                 }
             }
    
             // This script is called once per FixedUpdate frame if
             // the Velocity property's magnitude is > 1.
             private void ApplyVelocity()
             {
                 float speed = GetSpeed();
                 
                 if (Velocity.magnitude > speed)
                     Velocity = Velocity.normalized * speed;
                   
                 Vector2 position = Rigidbody.position;   
    
                 // This is the problem piece of code. While it retrieves the correct
                 // Rigidbody, position, and Velocity, the Rigidbody fails to move
                 // unless this script is called from inside the parent object. Additionally,
                 // calls to a method in the parent from this child's script will also fail
                 // to move the Rigidbody.
                 Rigidbody.MovePosition(position + (Velocity * Time.fixedDeltaTime));
     
                 if (Velocity.magnitude > 0.05f)
                     Velocity = Vector2.Lerp(Velocity, Vector2.zero, Friction * Time.fixedDeltaTime);
                 else
                     Velocity = Vector2.zero;
             }
    
             // Called from the parent script whenever input is recieved.
             public void Move(Vector2 vector)
             {
                 float multiplier = vector.magnitude / GetSpeed();
                 Vector2 targetVelocity = (Velocity + vector) * multiplier;
                 Velocity = Vector2.Lerp(Velocity, targetVelocity, Acceleration * Time.fixedDeltaTime);
             }
    
    
    

Parent Object (with "Player" parent script attached: alt text


Please let me know if it's possible to use a child script to call Rigidbody.MovePosition on a parent's Rigidbody2D. I'd very much prefer to keep my movement script in a child gameObject, but will merge it with the parent if it's an absolute necessity.


Child Object with "Movement" script (wherein the code above resides): alt text


TL;DR: MovePosition won't move a Rigidbody2D when called from a child gameObject. Is this an intended limitation? Does MovePosition have to be called from the same gameObject as the Rigidbody?


Thanks in advance for the help, and thanks for reading! :)

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 mangosauce_ · Mar 22, 2021 at 12:34 AM

The problem was staring me in the face! The attached "parent" script on the movement object was pointing to a script attached to the object's prefab. This erroneous parent script was of a different type to the Player script it was supposed to have as a parent. Hence, it didn't know whose frickin' Rigidbody to MovePosition. Duh!


The incorrect script attached as the parent: alt text


The correct script ("Player") attached as the parent: alt text


Another simple mistake I could've easily figured out if I knew where to look! I deserve an award or something (for tunnel-visioned-ness). :P

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 mrVentures · Mar 21, 2021 at 11:38 PM

I am not sure my friend. But have you considered adding an extra component to the parent? This could be a centralized input for external instructions on how it should move and it would not bloat the existing script.

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 mangosauce_ · Mar 21, 2021 at 11:52 PM 0
Share

I'll consider doing this if I still can't get the current system to work as intended. :D Thanks for your reply!

avatar image
0

Answer by Edy · Mar 21, 2021 at 11:47 PM

There's no such limitation.

Definitely there's something wrong in your side. I constantly modify the properties of the rigidbodies from different components along the child objects in the hierarchy.

Try creating the simplest test scene possible and use debug output to verify you're accessing the correct rigidbody and modifying their properties properly. When this works, add complexity progressively until your find the culprit of the problem.

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 mangosauce_ · Mar 21, 2021 at 11:54 PM 0
Share

Thank you so much for clearing that up! I'll absolutely do some more debugging to narrow down the cause of this issue. I just figured I'd throw down a question to see if what I'm trying is futile. Cheers! :D

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Recalculating collisions after Physics2D.IgnoreLayerCollision()? 1 Answer

How to make physics relative to moving parent 1 Answer

Rigidbody2D: Follow other Rigidbody2D strictly 1 Answer

Inconsistent gravity/random forces/solver problem? (forum crosspost) 0 Answers

Stack of RigidBody2D, Freeze X and Z, Odd Behavior 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