• 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 Christoff · Jan 20, 2011 at 04:34 PM · 2dmovementaxis

Restricting the movment in the Zaxis

Ok i dont want another look at this 2D game tutorial from unity.pdf that space game. But can someone please help me :)

All i want is no movment in the z axis , i still want the object to be able to rotate left and right but not backwards and forward .. etc so its true 2D movment..

just a simple script to apply?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by pfranza · Jan 20, 2011 at 08:19 PM

You can restrict the movement of a object within the Update() method

If you want to restrict the movement of the Z axis to be within certain bounds you can use:

function Update() {      
   transform.position.z = Mathf.Clamp(transform.position.z, -10, 10);
}

the above sample will restrict the z axis to be between -10 and 10

If you want to disable the movement of the object within a certain axis entirely you can do something like the following

function Update() {
   transform.position.z = 0;
}

This way after each update cycle regardless to what happened to that object the z position resets to zero. Movement to any axis can be restricted similarly.

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 Christoff · Jan 21, 2011 at 05:19 PM 0
Share

Ok thankyou that worked :) But if i want to stop for example a cube from rotating forwards and backwards, while still alowing it to rotate from left to right, how would i do this?!

would it be:

function Update() { transform.position.z = 0; transform.rotation.z = 0; }

avatar image
4

Answer by jonas-echterhoff · Jan 20, 2011 at 04:39 PM

Unity 3.2 will add built-in support for this in the Rigidbody component.

Until then, a simple script setting respective transform.position and transform.rotation coordinates to 0 (or whatever you want) in Update or FixedUpdate should do.

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

Help with 2D AI scripting 2 Answers

Moving camera by z axis in 2D 1 Answer

Player rotation affecting Top Down 2D Movement 1 Answer

Character movement problem, floats away 1 Answer

Making side-scrolling movement? 0 Answers


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