- Home /
Child object not moving with parent
Hi,
I'm sure im missing something obvious here, however I have trawled through every tutorial and post i can find but cant seem to crack it. Any help would be appreciated.
I have a parent object called "Ship" and a child called "Player". The end goal is for the ship to be able to move and be effected by physics, while the player can run around on deck.
In my current setup I cant stop the player sliding off the back of the ship when I add force. My understanding is that by making the player a child of the ship it should move with it, however the only way I have found to do this is to make both objects kinematic rigidbodies, which will cause me issues as I want the ship to be affected by physics.
The ship object has a rigidbody attached and I am moving it by adding force (the intent is for this to act like an engine):
void Update()
{
//Move the RigidBody forwards at the speed you define
m_Rigidbody.AddRelativeForce(Vector3.forward * thrust);
}
The player has a script attached that allows it to move/look around etc. The code for the actual movement is below, (obviously there is supporting code around this). It works fine for a normal FPS style game, however is possibly not the right method for this case.
The player does not have a rigidbody attached.
characterController.SimpleMove(Vector3.ClampMagnitude(fwdMovement + rightMovement, 1f) * _speed);
Answer by golaod · Jun 09, 2021 at 08:00 PM
It's an old topic, but I just had the same issue. I moved my old project to new unity and new render pipeline system and same script, same player and same platform stopped working.
After checking all the topics with rigidbody, OnTriggerStay, position correction and so on, I found out that there is 'auto sync transforms' option which was not enabled in my new project. When I reenabled it, things went back to normal. (Edit -> Project Settings -> Physics (or Physics 2d) -> Auto Sync Transforms
Thanks, this is exactly what I needed! I haven't seen anyone else in any forum mention Auto Sync Transforms, and I'd wasted at least an hour trying to figure out why my object children weren't moving with their parents.
Answer by HellsHand · Apr 28, 2021 at 10:15 PM
A suggestion that may be worth trying, adding force to both the player and the ship at the same time when the ship moves, though you may have to play around with the amount of force for the player.
Thanks for the reply.
I struggled with adding force in the same direction, it was very unpredictable and in many cases would not add force at all (I added a rigidbody to the player).
However it got me thinking about using the transform function to always keep the player on top of the ship by adding its transform to its own.
Theres definitely a more elegant way of writing the code below, but im keeping it longhand for now to try and help visualise whats happening:
This keeps the player nicely on top of the ship, however stops you moving entirely from that point as you always reset to 0:
transform.localPosition += new Vector3(0,0,0);
So I have tried this, however this fires you into the distance as its always adding. I think i need to find a way of storing the local position separately and then use that. Possibly that will require a re-think of how the character moves, but potentially I could move the character in local space and then just add the current location of the ship to that.
transform.localPosition = new Vector3(parentShip.transform.position.x + transform.position.x, parentShip.transform.position.y + transform.position.y, parentShip.transform.position.z + transform.position.z);
Answer by cotterdev · Jun 09, 2021 at 11:59 PM
When adding force, this should be done in FixedUpdate instead of Update. Anything to do with physics should be executed in FixedUpdate.
Your answer
Follow this Question
Related Questions
How to make a rigidbody move in the direction of finger(x,y,z) 1 Answer
Implementing Counter-Movement 0 Answers
How do I stop unwanted rotations? 4 Answers
How to move Character with AddForce 1 Answer