- Home /
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?
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
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);
}
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...."
Thanx for your answer. I think you´re right. Collision box might be the best way to do this.
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);
}
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.
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.
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 =]
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.
did you try the Clamp script on my answer? I did test this with the generic Unity FPController.
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
![](https://koobas.hobune.stream/wayback/20220613074655im_/https://answers.unity.com/themes/thub/images/avi.jpg)