• 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
0
Question by tankerxiaodi · Oct 31, 2010 at 08:13 AM · 2drigidbodygame

How can I prevent GameObject rotating around the Z axis?

Hi, Sorry for my poor English. I'm going to create a 2D game, so I add a Cube into game, and add RigidBody in it. hope it only rotat around X and Y axis.

How can I do that?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by _Petroz · Oct 31, 2010 at 11:19 AM

If you are making a 2D game I presume you only want it to rotate about one axis, namely the one toward the camera. That is the problem I am currently facing. I'm not sure if there is a better way but this is what I am currently doing:

if (transform.up != Vector3.up)
{
    transform.LookAt(transform.position + transform.forward, Vector3.up);
}

When the object's 'up' is not the world up then make the objact 'look at' the current direction it is facing and reset the up to the world up.

Hope this helps.

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
0

Answer by tankerxiaodi · Nov 02, 2010 at 03:04 AM

Thank you Petroz, I found these codes work for me:

public var forceBetweenXYZ:String="xy"; private var Axis:Vector3;//desired depth private var Rotation:Vector3;//desired rotation

function Awake() { Axis=transform.position;//defines the starting X position as the desired X position Rotation=transform.eulerAngles;//defines the starting rotation as the desired rotation } function Update() {//each frame var spinforce:Vector3=gameObject.rigidbody.angularVelocity;//is the rotational force on this frame var place:Vector3=gameObject.transform.position;//is the target position in world space on this frame

 if(forceBetweenXYZ=="x")
 {
     if(place.x!=Axis.x)
     {//if the current X position IS NOT the desired X position then...
     transform.position.x=Axis.x;//go to the desired X position
     }
     gameObject.rigidbody.angularVelocity.y=0;//stop rotating around y axis
     gameObject.rigidbody.angularVelocity.z=0;//stop rotating around z axis
     gameObject.transform.rotation.y=Rotation.y;//resets it to the desired y rotation
     gameObject.transform.rotation.z=Rotation.z;//resets it to the desired y rotation
 }
 else if(forceBetweenXYZ=="y")
 {
     if(place.y!=Axis.y)
     {//if the current X position IS NOT the desired X position then...
         transform.position.y=Axis.y;//go to the desired X position
     }
     gameObject.rigidbody.angularVelocity.y=0;//stop rotating around y axis
     gameObject.rigidbody.angularVelocity.x=0;//stop rotating around z axis
     gameObject.transform.rotation.x=Rotation.x;//resets it to the desired y rotation
     gameObject.transform.rotation.z=Rotation.z;//resets it to the desired y rotation
 }
 else
 {
     if(place.z!=Axis.z)
     {//if the current X position IS NOT the desired X position then...
     transform.position.z=Axis.z;//go to the desired X position
     }
     gameObject.rigidbody.angularVelocity.y=0;//stop rotating around y axis
     gameObject.rigidbody.angularVelocity.x=0;//stop rotating around z axis
     gameObject.transform.rotation.y=Rotation.y;//resets it to the desired y rotation
     gameObject.transform.rotation.x=Rotation.x;//resets it to the desired y rotation
     if(forceBetweenXYZ!="z")
     {
         print("FORCING Z BY DEFAULT, USE X, Y, OR Z LOWERCASE");
     }
 }

}

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
3

Answer by Peter G · Dec 01, 2010 at 01:54 AM

Since you are using a rigidbody system to control you object. A configurable joint would make sense here. I know they are OVERWHELMING, but you only need to change 1 setting:

Angular ZMotion = Locked.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

rigidbody.useGravity doesnt work? 1 Answer

2D Game, Character Slides off Slopes, Need to be Able to Walk on Them 1 Answer

Switching Between "Dimensions"? 1 Answer

2D Mouse Look with Collisions 1 Answer

Help on a 2d game. 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