- Home /
Child affecting parent rigidbody?
I have a GameObject hierarchy setup like this:
- Player (Physics)
- Player (Graphics)
- Camera
- 1stPersonGun
Now the physics part of the player is not a character controller, but a rigid body (because I require more than just the y axis of rotation). When I press 'w', the game should add a force to the rigidbody so that it moves towards wherever its forward is (it is a relative force of Vector3.forward).
The Physics is rotated along its y axis using mouse input, and the camera is rotated along its x axis also using mouse input.
My problem is, when the camera is looking down, the added force is very low, and when the camera looks up, the added force is very high. When I detach the camera from the hierarchy the force application behaves fine, so why does the camera being a child affect the parent rigidbody?
If I need to give any clarification, let me know - it was kind of hard to explain.
Here is my physics code:
public Transform Player;
void FixedUpdate () {
Rigidbody mainPlayer = Player.rigidbody;
if (Input.GetKey("w"))
{
mainPlayer.AddForce(Player.TransformDirection(Vector3.forward) * 20, ForceMode.Acceleration);
}
else if (Input.GetKey("s"))
{
mainPlayer.AddForce(Vector3.zero - (Player.TransformDirection(Vector3.forward) * 20), ForceMode.Acceleration);
}
else
{
if(_ContactPoints.Count > 0)
mainPlayer.velocity = Vector3.zero;
}
}
Does 1stPersonGun have a rigidbody associated with it?
Are you getting your relative forward vector based on the Camera's space at all?
No, it doesn't
And I don't think so. I am getting the player physics rigidbody, then doing AddRelativeForce(Vector3.forward, Force$$anonymous$$ode.Acceleration);
Answer by whydoidoit · Jul 13, 2012 at 07:26 AM
I'd guess we'd need to see the code for the physics - but as a guess without it - your 1stPersonGun has a collider attached to it. In which case the collider acts as a part of the parent rigidbody's physical structure and will alter the forces applied to it significantly. This would explain why different angles affected the force differently as the gun is changing its position as the camera moves and hence altering the shape of the object onto which you are applying physics.
Do one of these:
Don't have a collider on the gun (or the camera)
Keep the camera and gun separate
Extend CharacterController to give you the effects you want - very hard to do it perfectly with physics (but of course possible).
EDIT The final solution to this was to make sure that physics code refers to the transform.forward of the actual item with the rigidbody.
I don't have a collider on anything but the 'Player (Physics)'
The gun isn't the problem. I have gone through a large amount of testing, and I rarely come to UnityAnswers unless I have tried everything I can think of. The camera is the problem.
I don't want to use CharacterController because it doesn't make any use of AddForce or AddRelativeForce, which I need for my game.
$$anonymous$$y physics code is now in the main question.
So could you:
Post your physics code by editing the question
A screenshot of the hierarchy would be useful
Ok I have added the physics code and hierarchy picture to the bottom of the question.
Just a couple of things on that code (which isn't the problem by the look of things!):
Cache the rigidbody in Awake or Start it will be much faster
You can use Player.forward rather than calling TransformDirection(Vector3.forward)
Under $$anonymous$$usket - you are absolutely sure that there is nothing with a collider (nor does the camera have one?). Because that would totally explain it... Your physics works well
Ok thanks for the 2 tips.
I just tried moving the camera out the hierarchy, and rotating it along the x axis still somehow affects where this 'Player.forward' is. The only thing that could possibly be affecting it is the fact that the physics code is on the camera and not on the player. I will try a few more things out - if you have any suggestions please tell me :)