• 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 jmendegan · Dec 19, 2011 at 06:33 AM · charactercontroller2.5dside scroller

How do you lock the first-person CharacterController to only walk along the x-axis?

I am making a 2.5D sidescroller and want to know if there is a way to take the built-in CharacterController and either: 1. Lock the movement to be only on the x-axis and y-axis(so that the character only moves left and right and may jump) or 2. Remove the keys W and S and UP and DOWN so that there can be no movement on the z-axis. I have read other "fixes" where people place invisible walls so that the character is restricted from moving along the z-axis, but surely there is a real fix to the problem. THANKS!

Comment
Add comment · Show 1
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 syclamoth · Dec 19, 2011 at 06:49 AM 0
Share

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by aldonaletto · Dec 20, 2011 at 05:29 AM

You should modify the CharacterController movement script. If you're using the standard First Person Controller, open the CharacterMotor.js script and find t$$anonymous$$s instruction:

       // Move our character!
    movement.collisionFlags = controller.Move (currentMovementOffset);
Add one instruction to zero the z movement before Move:

       // Move our character!
    currentMovementOffset.z = 0; //<- zero movement in the z direction
    movement.collisionFlags = controller.Move (currentMovementOffset);
But be aware that CharacterMotor.js is a big monster that eats experienced programmers in the breakfast! Most modifications only result in undesired collateral effects, thus t$$anonymous$$s one may also have weird consequences!
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 BiG · Dec 19, 2011 at 07:08 AM

You would be interested in the "Constraints" section of the "Rigidbody" voice, in the Inspector. Look at here: http://unity3d.com/support/documentation/Components/class-Rigidbody.html. Search for the voice "Freeze position", and make sure that you've only the check on Z-axis, deactivating X- and Y- ones. However, t$$anonymous$$s works for physics, and for forces related to it: if you have a script that move the character using WASD, or arrows, movements along all these direction still remain valid. Reading at your question, it seems that you are using the "3d person controller", that's implemented by default on Unity. My advice is to make a script of yours, very easy indeed. Look at t$$anonymous$$s:

 var speed = 20.0;
 
 function Update () {
    var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
    var y = Input.GetAxis("Vertical") * Time.deltaTime * speed;
    transform.Translate(x, y, 0);
 }

The script above make the spaces$$anonymous$$p controllable only on X- and Y- axis, using arrows or WASD. Hope that it helps.

Comment
Add comment · Show 3 · 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 syclamoth · Dec 19, 2011 at 07:14 AM 0
Share
avatar image jmendegan · Dec 20, 2011 at 03:03 AM 0
Share
avatar image BiG · Dec 20, 2011 at 08:50 AM 0
Share

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a character jump with the Character Controller Component instead of Capsule Collider 0 Answers

2.5 D beat em up 1 Answer

Using CharacterController.Move to move character to specific point 2 Answers

Keeping the player on a moving platform? 0 Answers

character controller minimum width of walking platform 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