- Home /
Children ignoring Parent control. [Solved]
I asked this question on the forums but it seems to have been lost at sea, and considering it's something of a question that shouldn't require debate I figured this would be a better place to ask. I've poured over everything I could find (forum threads, other questions here) about rigidbodies and parentage but I haven't found much, some stuff about how you shouldn't put rigidbodies on children (in other situations) but the docs said it was good so here goes.
Setup
I am developing a game set in space and would like to move the "world" around the player rather than the other way around because of floating point errors that start happening around 10K units. To accomplish this I have set up nested coordinate systems using parenting.
I have a rigid body called the Local Rotation Group that remains stationary at the origin with the player. It has a rigidbody so I could use torque and drag to better simulate player rotation. This works exactly as expected.
I have another rigid body called the Local Translation Group. This object is used to simulate translational movement of the player (ie: player pushes forward, the translation group moves backward). This also works perfectly.
Combined these should simulate the player moving and rotating around the level.
Problem
However, the translation group stops responding to the rotation group's (it's parent) rotation when the script controlling the translation is turned on. If disabled before play, all objects in the translation group rotate as expected, but when the script is enabled they snap back to their original position and no longer rotate around the player (although movement controls still work), upon disabling the script they once again respond to rotation. If enabled (with the addrelativeforce function commented out) again the translation group rotates with the parent.
In the docs on rigidbodies the section on Parenting describes the exact effect I'm going for but it doesn't seem to work. Everything in the scene has a rigidbody. The offending code (and line I mentioned commenting out) seems to be:
//Translation Group code
void FixedUpdate()
{
float input = Input.GetAxis("Forward Thrust");
if(this.Invert == true)
{
input *= -1;
}
this.rigidbody.AddRelativeForce(input * Vector3.forward * this.ForwardThrust);
}
For completeness:
//rotation group code void FixedUpdate () { rotationX = Input.GetAxis("Mouse X") * sensitivityX; rotationY = Input.GetAxis("Mouse Y") * sensitivityY; if(this.InvertY == false) { this.rotationY *= -1; }
if(this.InvertX == true)
{
rotationX *= -1;
}
Vector3 t = new Vector3(rotationY,rotationX, 0);
this.rigidbody.AddRelativeTorque(t.normalized*this.Torque);
float input = Input.GetAxis("Roll");
if(this.InvertRoll == true)
{
input *= -1;
}
this.rigidbody.AddTorque(0,0,-1 * input * this.RollThrust);
}
Hierarchy:
(the cubes are so I can see what's happening in each group and contain rigidbodies)
Question
Is there a solution? Is this even the right approach? If any clarification is needed please let me know.
Cheers and Thanks!
Just realized I'm doing too much work in the rotation group code, not that it solves the problem...
Are you using localPosition? That'll make sure movement is relative to the parent
Are you using 2 rigidbodies? They will behave independently of each other, I'm not sure what you want to achieve with this.
Try adding a hinge joint to restrict motion to a single axis on the child
@Spinaljack: I was hoping to handle everything through the physics system. From what the docs say if a child of a rigid body has a rigid body the child with get dragged along with the parent but it's collisions will still happen regardless of the parent. Which is exactly what I was going for, but it doesn't seem to work. The objects that do the rotating and translating don't have colliders, they're just used as origins for their respective spaces.
I've never seen localPosition, I'll have to look into that and let you know.
Answer by Mike 3 · May 08, 2010 at 02:34 PM
won't this approach just give you floating point precision problems in reverse?
when your playzone is 10k units away, you'll start hitting issues.
the usual technique is to have your ship move like normal, but when it hits a boundary (say 1k units), it moves both the play area and the player back by that value, and then store what global space you're in
that way your universe is segmented into smaller grid blocks, which can help in many other ways too
Yep, but I was hoping that if it wasn't happening right in front of the player they wouldn't notice if the physics isn't behaving correctly.
The method you described is what I just switched over to and it seems to work perfectly except the trail renderer leaves a nice long trail to the middle of nowhere when the boundary is hit.
Just one question: "then store what global space you're in" Do you mean what grid space? I came up with a sneaky way of never having to actually deal with that :)
Almost forgot, regarding the child/parent rigidbody problem, am I crazy or do the docs say what I was doing should have worked?
to be honest i'm not sure - you're using non kinematic rigidbodies for the entire universe which is really going to go creepily wrong with collisions, especially if you have other objects moving with rigidbodies inside
Your answer
Follow this Question
Related Questions
Rotating sphere with rigidbody attached 1 Answer
How can I rotate and move a cylindrical rod from one side with respect to other side? 1 Answer
why can't my Rigidbody not rotate on slopes? 1 Answer
c# problem with limiting rotation (with rigidbody) 1 Answer
How do I create a camera similar to the one in th room of shadows example? 0 Answers