Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by spaceshooter · Dec 15, 2010 at 07:40 PM · 2dcollisionphysics2d-physics

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by TowerOfBricks · Dec 15, 2010 at 08:06 PM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pe-ads · Jan 08, 2011 at 02:43 PM 0
Share

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.

avatar image
2

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,

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

1 Person is following this question.

avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges