- Home /
Physics in Local Space rather than World Space
Hey guys, I'm working on an endless runner game. Usually with those the idea is to leave the player stationary, and move the level geometry instead. Now the client has thrown a bit of a curve ball and asked for physics bodies in the level, like a stack of boxes or dangling chains.
The problem is, I move the level using transform.position = Vector3.left * speed on each level "tile" that I spawn. This causes any physics bodies i place in the tile to move around as the level moves.
Is there a way to mark certain physics volumes as relative to their parents transform rather than world space? Or can you think of any other ways to combat this problem? Do I need to change the level structure so that it actually is the player that moves rather than the level?
Answer by Owen-Reynolds · Jul 14, 2014 at 07:40 PM
I've gotten surprisingly good results with childing rigidbodies and moving the parents.
I noticed it after trying to make a quick example of "why you should never make a child be a rigidbody." In some cases, I could not make child RBs misbehave. Turns out that moving an empty parent of several RBs works fine. It's just a quick way of manually moving them all (with no change to their speed.)
Since you move everything, an object will never teleport inside another this way, only into the player. And player-hits are probably already handled in a special, non-physcis way (by you.)
[Edit] If the objects are resting when you do this (or stacked,) the moving "floor" needs to be a kinematic rigidbody (credit to OP, I don't know why this works.)
I tried this and unfortunately it creates weird physics behaviours. Would you $$anonymous$$d elaborating on how you've gotten decent results doing this? I tried dragging out a long kinematic box as the ground, then stacking some physics boxes one on top of the other, then parenting everything to a root transform. What happened was the blocks slid over the ground as if I was "pulling the rug out from under them" and eventually toppled over.
Never actually tried this approach, but I don't see why it won't work.
The only time I ever get rigidbody parenting problems is if one rigidbody is a parent of another rigidbody
This actually did work. The problem I was having before was due to not adding a rigid body to my ground volume. I thought I had done that but it was just a regular collider. Once I added that, the physics behaved correctly.
Wow! I just noticed the same problem you had, and realized I had only tested this with moving rigidbodies. Never on resting, and it completely didn't work them them -- the sliding problem you noticed. I never would have guessed giving the base a kinem RB worked.
Answer by Kiwasi · Jul 14, 2014 at 07:27 PM
Physics works fine if you simply add your move level script to every GameObject that needs to move with the level. I've done the same thing in my side scroller. I would suggest not parenting the RigidBody, as this can cause weird effects.
In other words use transform.position to keep all of your GameObjects moving past the camera. Then use rigidBody.AddForce to get realistic physics movement on top of the base movement.
...and to be more specific on that 1st sentence, suppose an object is falling straight down. If it gets non-physics moved (translate, position=, move parent... ) the physics speed won't change. If will still be falling straight down. If won't "see" the sideways motion and turn that into a diagonal fall.
Now, if the level spins you have a problem, but even then, I think spinning the velocity will work.
Interesting. I'll have to try that out. I tried calculating a velocity that is counter to how fast the level is moving and applying that to each rigid body, but that didn't work. Never thought of moving each object individually though. Sounds so crazy that it just might work.
Your answer
Follow this Question
Related Questions
Rotate and transform in local space 2 Answers
ignorecollision return error 0 Answers
How to disable and enable HingeJoint? 0 Answers