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
0
Question by Flipbee9 · Jun 26, 2010 at 01:46 PM · 2dlockz-axis

2D - Locking The Z Axis

So, I'm making a 2D Platformer. I've been able to add some nifty features, but lately I've run into a problem. I would like my character to only move on the X and Y axis without any movement in the Z axis. Everytime I collide with a box, my character falls off the platform on the Z axis which I don't want. I've heard about configurable joints, but I'm still unclear about this all.

Thanks In Advance

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

4 Replies

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

Answer by Peter G · Jun 26, 2010 at 01:55 PM

This question has been answered in multiple places before. There are 2 common solutions. One, in your player code, every frame call:

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


//C#
//using UnityEngine... public class...
void Update () {
     Vector3 pos = transform.position;
     pos.z = 0;
     transform.position = pos;
}

or you can add a configurable joint. Then go to z motion and choose locked instead of free or limited.

Other links.

Locking desired axis

Comment
Add comment · Show 5 · 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 Flipbee9 · Jun 26, 2010 at 01:57 PM 0
Share

Thanks for the quick response :D

avatar image Cyclops · Jun 27, 2010 at 06:26 PM 0
Share

@Peter G, one $$anonymous$$or point - that's Javascript. In C# you can't assign to Z, you have to do: transform.position = new Vector3(x, y, 0)

avatar image Peter G · Jun 27, 2010 at 08:06 PM 0
Share

Right, I'll post the C# version. $$anonymous$$ost newcomers seem to choose JS because all the documentation is JS so I write it as such.

avatar image Cyclops · Jun 27, 2010 at 10:38 PM 1
Share

@Peter G, true, I wasn't suggesting you change the post, I figured anyone seeing the post would also see my comment, which would be enough notice. :)

avatar image AntFitch · Mar 26, 2012 at 10:36 PM 0
Share

The C# script completely froze my character so that it wouldn't move in any direction. Anyone know why? I created a C# script with only the contents above and attached it to my gameObject (character controller).

avatar image
1

Answer by DragonGamingYT · Jul 17, 2016 at 07:47 PM

thanks man this helped me so much since my game had a majour bug that made it that if you landed right you'd be sendt off of the map so I had to add barriers but you could use those barriers to jumo infinitely

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 Gumpert · Sep 29, 2013 at 06:14 PM

Hi, I'm trying to do a similar thing, but I'm just wondering if its possible to make a script that instead of saying "continuously set z to 0" it would say something like "the z can never change to be anything other than 0 in the first place". Its because if I use this script and I walk into a collider thats facing diagonally (i'm making a 2.5d game, and ill try to not have any diagonal colliders, but it might not be completely possible), you can glitch through it, and it goes all messed up.

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 soxroxr · Sep 20, 2014 at 10:12 PM 0
Share

I experienced this as well, and it's quite unsatisfactory. I haven't came up with a solution of my own, but I've read that rigidbody components have a mechanism to prevent movement in any of the axes. I'm just not very interested in using one as the games I'm making don't use physics, seems like needless overhead.

I'd love to hear an answer on this topic, anyone out there have a good solution?

avatar image
0

Answer by toniepeli · Jun 22, 2016 at 08:37 PM

See http://forum.unity3d.com/threads/restrict-z-movement-on-character-controller-without-breaking-into-colliders.91358/

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Lock Z rotation to 0 2 Answers

Locking the Z-axis based on direction 0 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

make object rotate towards its direction after bounce 1 Answer

GameObject leaves camera area by change its z value to a defult ( canvas is in screen space-camera mode) 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