- Home /
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:
Using MovePosition (Didn't work.)
Setting the Rigidbody's velocity (Didn't work.)
Using AddForce (ForceMode2D Impulse & Force) (Didn't work.)
Calling a method in the parent script (from the child) to move the Rigidbody based on the child's Velocity property. (Didn't work.)
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:
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):
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! :)
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:
The correct script ("Player") attached as the parent:
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
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.
I'll consider doing this if I still can't get the current system to work as intended. :D Thanks for your reply!
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.
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
Follow this Question
Related Questions
Stack of RigidBody2D, Freeze X and Z, Odd Behavior 1 Answer
Is it possible to disable collisions for separate physics scene? 0 Answers
increasing knockback of a rigidbody as received damage increases (like super smash) 1 Answer
Recalculating collisions after Physics2D.IgnoreLayerCollision()? 1 Answer