- Home /
Limiting Physics to X and Y
Is there a way to have physics collisions only effect the X and Y axis? In this 2D setup, I'd like them to bounce off each other, but their Z value to remain unchanged. Thanks!
Answer by Statement · Dec 15, 2010 at 07:50 PM
Is there a way to have physics collisions only effect the X and Y axis?
Yes, however I've found it very hard to get a stable solution for 2d physics. Basically I use a combination of configurable joints to lock movement in Z and invisible box colliders to trap the objects in a plane. The colliders should have a physics material that is slippery so objects wont get stuck in air. Occasionally I've tried to set Z = 0 in FixedUpdate but any of these solutions will produce buggy physics if off-center collisions occur in the Z axis.
For a game I was partaking development in (Bob Came In Pieces) we used combinations of the above to lock gameplay to 2d. If you play it and smash really hard against walls you can see that physics can cause tunneling and other Z-related glitches such as overlapping and "space fighting". Luckily it isn't very noticable but it is extremely hard to get 2d physics "just right" using the built in physics engine.
I don't know if there are third party 2D plugins for Unity but if there are I'd strongly advice you to check them out. Another option is to write your own collision detection and response system, but it's a lot of hard work and I doubt that's the answer you'd like to get.
While it's hard to get 2D physics "just right" with the built in 3D physics engine, you can still give it a go. It's not impossible to create 2D gameplay, just know that it can be hard.
Answer by TowerOfBricks · Dec 15, 2010 at 08:06 PM
There is a "Lock To 2D" helper script in the Lerpz 2D example project
Whereabouts? I can't find any script named that in the assets folder... nor anything in the instructions except for the old configurable-joint method.
Answer by doomprodigy · Dec 16, 2010 at 12:15 AM
I found this lying in scripts ive downloaded as I started learning unity maybe it can help you.
//this script stops the physics object rotating on it's X and Z axis, it also stops it moving off the Y axis
void Update ()
{
transform.eulerAngles = new Vector3(0,transform.eulerAngles.y,0);
transform.position = new Vector3(transform.position.x,0,transform.position.z);
}
}
Add it to any collider, it should work.
This is fully 2d not 2.5 were talking right..
Peace,
Answer by lajos · Jul 07, 2011 at 01:51 AM
You don't need a script for this in unity 3.3.
On the Rigid Body component there is a section called Constraints to freeze position and rotation along the desired axis. Check the axis you want to disable, for example position.Z, rotation.X and rotation.Y.
In my tests it's much more stable than doing it from a script.
Answer by Pe-ads · Jan 08, 2011 at 02:55 PM
Altering Aaron Lewis' code a bit, I came up with this JavaScript:
//This script restricts movement and rotation in the z-axis. Enjoy! function Update () {
transform.eulerAngles = new Vector3(transform.eulerAngles.x,transform.eulerAngles.y,0);
transform.position = new Vector3(transform.position.x,transform.position.y,0); }
Works like an absolute dream! No need to mess around with configurable joints, etc. Not sure if it would slow down the processor or not with this script attached to lots of objects.
Thanks a lot, Aaron!
Pe-ads
Your answer
Follow this Question
Related Questions
Why is there no Rigidbody2D.SweepTest() ? 1 Answer
Can I detect the impulse of a 2D Collision? 0 Answers
Is there a solution to when colliders bypass? 2 Answers
Creating 'redstone' like wires 1 Answer
Smooth box to curve collision in 2D 1 Answer