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 Frankiej · Jul 25, 2012 at 12:33 PM · movementfirst person controller

Stop character from moving.

Using the "First Person Controller" and i don´w want the user to be able to pass a certain transform.position.x.

What is the best way to achieve this? I could make a HUGE collider box to intercept the user. But that doesn´t seem good. A better way would to check if the user has passed x cord and then do something right?

How could i achieve this?

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
1
Best Answer

Answer by AlucardJay · Jul 25, 2012 at 01:02 PM

You could use the conditional check if(transform.position.x < xLimit) as suggested, or clamp the values (straight from the docs) :

 transform.position.x = Mathf.Clamp(transform.position.x, -3.0, 3.0);

This makes sure the value is always between the min and max floats that are set in the command. More info here :

http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Clamp.html

Comment
Add comment · Show 4 · 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 AlucardJay · Jul 25, 2012 at 01:51 PM 0
Share

I have just tested the First Person Controller by making the following script and attaching it to the FP Controller. The Controller does not move less than -3 or more than +3 on the X-Axis, and there was no problem at all with receiving inputs and moving in a direction that wasn't restricted. Did you actually try this? Attach this script to the FP Controller :

 #pragma strict

 function Update () {
     transform.position.x = $$anonymous$$athf.Clamp(transform.position.x, -3.0, 3.0);
 }
avatar image AlucardJay · Jul 25, 2012 at 01:55 PM 0
Share

anyhow, just scaling a single cube (with box collider and renderer enabled set to false) would solve the problem, then on the cube you could have an OnCollisionEnter to post your message "Don't go in the woods...."

avatar image Frankiej · Jul 26, 2012 at 08:53 PM 0
Share

Thanx for your answer. I think you´re right. Collision box might be the best way to do this.

avatar image AlucardJay · Jul 26, 2012 at 10:37 PM 0
Share

Hey, I have just tested this again, and was actually WRONG. You only need to clamp the X. I am very sorry, the answer has now been updated. If you make a blank script, add this code, then attach it to the char controller, it will DEFINITELY let you move in the Y and Z axis, but only in the X-axis between -3 and 3. Again, really sorry for giving a wrong answer, but this is properly tested now. I know I've given you the run-around, but please try this, i think you'll finally be happy with it (Working) !

 #pragma strict

 function Update () {
     transform.position.x = $$anonymous$$athf.Clamp(transform.position.x, -3.0, 3.0);
 }
avatar image
0

Answer by Piflik · Jul 25, 2012 at 12:42 PM

Well...you could add (if transform.position.x < xLimit) in the script before you get the player input, but personally I would use colliders.

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 Frankiej · Jul 25, 2012 at 01:43 PM

Thx for your answers.

The problem i´m having is that if i do this my character will get stuck and recieve no input. I would still want my character to be able to move in the other direction.

The actual game problem is this. User starts with a road ahead of him. To the left is woods (with no collisions on the many....many trees). So i don´t want the user to go into the woods and find out i´m cheating a little bit for performence sake. So my plan was when the user enters the wood a text will be displayed and the user can´t go into the woods.

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 AlucardJay · Jul 25, 2012 at 02:01 PM 0
Share

Please post comments by clicking the [add new comment] button, a window will pop-up to type in (then delete this answer). The answers are reserved for answers only, thanks =]

avatar image Mander · Jul 25, 2012 at 04:19 PM 0
Share

invisible walls would be the answer to ur problems imo.

avatar image Frankiej · Jul 26, 2012 at 08:25 AM 0
Share

Yes, i could be an invisible wall with a collision box. But it have to be HUG$$anonymous$$ I found it better to just check the players x position but i can´t get it done correctly.

avatar image AlucardJay · Jul 26, 2012 at 08:33 AM 0
Share

did you try the Clamp script on my answer? I did test this with the generic Unity FPController.

avatar image Frankiej · Jul 26, 2012 at 08:52 PM 0
Share

Thanks for your answer alucardj. Tried it. Didn´t get it to work. $$anonymous$$y Z and Y position was going to be at 0. I want the character to be able to move in that directions...but i would like more controll over the X position. But i see some problems even if i do get it to work. And i think that your earlier suggestion on a HUGE collision box may be the best way to go.

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

8 People are following this question.

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

Related Questions

First person diagonal movement problems 0 Answers

Way to set first person controller velocity? 1 Answer

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

Simple first person controller camera rotation 0 Answers

Making a bubble level (not a game but work tool) 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