• 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 Leroterr · Jun 21, 2014 at 05:11 PM · movementtransformcontrollermathfclamp

Make my player who uses Character Controller to stop moving in one certain direction once it reaches the borders.

My player uses the Character Controller component to move and I wanted to stop its movement once it reaches the border.

For example, when my player reaches the very left side of the screen, I want the Character Controller to stop moving on the left side, but it can still keep going upwards, downwards, and to the right. My character uses this code:

 charController.Move (((player.forward * joyDelta.z + player.right * joyDelta.x)) * player.speed * moveSpeed * Time.deltaTime);

When I try this, however:

 if(GameObject.Find("Player").transform.position.z > -100 && GameObject.Find("Player").transform.position.z < 100)
     {
         charController.Move (((player.forward * joyDelta.z)) * player.speed * moveSpeed * Time.deltaTime);
     }
  
 if(GameObject.Find("Player").transform.position.x > -100 && GameObject.Find("Player").transform.position.x < 100)
     {
         charController.Move (((player.right * joyDelta.x)) * player.speed * moveSpeed * Time.deltaTime);
     }

When it reaches the very left side of the screen, it will stop going through the left, but it will also stop going through the right. The same goes for the ups and downs.

Is there a way I can do this?

I wanted to only disable one direction. So if it hit the Northern Border, the controller will only disable the upwards movement, so it couldn't go upwards anymore, but it can still go downwards, eastwards, and westwards.

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

1 Reply

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

Answer by KilomanX · Jun 22, 2014 at 01:44 AM

in your code you only allow movement when the player is between -100 and 100 but you need to also let the player go back when he is outside, so if he is at the north border allow movement only if the movement is to the south, so we have: if(player is not at the north border OR movement is to south), we do this for south border too.

 var movementZ=((player.forward * joyDelta.z)) * player.speed * moveSpeed * Time.deltaTime);
 if((GameObject.Find("Player").transform.position.z > -100 || movementZ>0) && (GameObject.Find("Player").transform.position.z < 100 || movementZ<0))
         {
             charController.Move (movementZ);
         }
      

(I made a variable for movement for better readability)
I think that can help you, do it for east-west too.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How can i write this Code with Character Controller -1 Answers

Make my player who uses Character Controller to stop moving in one certain direction once it reaches the borders. 1 Answer

Finding local position and clamping movement 2 Answers

Why does the player keep pointing in a specific direction after moving? 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