Physics relative to parent
Hello everyone,
I've only just started using Unity, but I've been programming for around 6 years in various other game engines. I'm loving the speed of work in Unity and the simplicity compared to UE4. I'm experimenting with what kinds of things I can make Unity do, and this is one thing I've not successfully done in any game engine except jMonkeyEngine3
The goal is that physics are calculated with and applied to the object's local position rather than global. The idea is to simulate artificial gravity on a space ship, so rigid bodies on a ship could remain stationary or be moved naturally inside the ship, ignoring its orientation and velocity. I've tried parenting the rigid bodies to an empty GameObject (which I know is advised against), and I also wrote a script that applies the velocity of the ship to the rigid body, but this seems to be a little more complicated than it could be and also doesn't work well with angular velocity.
My next idea is to simulate the physics on dummy game objects (basically just a GO with a collider and rigid body) at the world origin, and apply their calculated world position to the local positions of the objects (just a mesh) on the ships. I'm just concerned that with multiple ships in a scene, this will become very convoluted. It couldn't be any more convoluted than writing my own physics engine though.
This idea isn't exactly a requirement to build my game, I'm just experimenting with Unity and seeing what can be done. I just thought I'd ask if anyone had any input on the topic. Thanks in advance!
Answer by BornInABottle · May 22, 2018 at 10:39 AM
Hi @lyonb96 I had a similar question here: https://answers.unity.com/questions/1508998/how-to-make-physics-relative-to-moving-parent.html?childToView=1509227#answer-1509227
Solution is to have the "ship" parent and "objects on the ship" parent separate in the hierarchy, then adjust the "objects on the ship" parent position & rotation every frame using Update() to match the "ship" parent.
Full answer via the link above.
Your answer
Follow this Question
Related Questions
Character bounces off walls, and drops to the ground on movement 1 Answer
Problem. Pick up and Grab object script, except all objects in scene are picked up instead of one. 0 Answers
Ray cast to object to transform, instead all objects with script transform. 0 Answers
Player rotates spastically on all axes when camera is turned 0 Answers
Why my gravity direction is rotating with transform.rotation? 1 Answer