x


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!

more ▼

asked Dec 15 '10 at 07:40 PM

spaceshooter gravatar image

spaceshooter
249 49 55 69

(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

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.

more ▼

answered Jul 07 '11 at 01:51 AM

lajos gravatar image

lajos
130 5 5 11

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 15 '10 at 07:50 PM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

(comments are locked)
10|3000 characters needed characters left

There is a "Lock To 2D" helper script in the Lerpz 2D example project

more ▼

answered Dec 15 '10 at 08:06 PM

TowerOfBricks gravatar image

TowerOfBricks
3.2k 17 25 50

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.

Jan 08 '11 at 02:43 PM Pe-ads
(comments are locked)
10|3000 characters needed characters left

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,

more ▼

answered Dec 16 '10 at 12:15 AM

doomprodigy gravatar image

doomprodigy
557 22 26 31

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jan 08 '11 at 02:55 PM

Pe-ads gravatar image

Pe-ads
63 3 4 10

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2483
x1865
x1032
x10

asked: Dec 15 '10 at 07:40 PM

Seen: 4280 times

Last Updated: Jul 07 '11 at 01:51 AM