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!