- Home /
Restrict character's movement within a level's map
Hi everyone !
I'm making a level and using a First Person Controller as my character. However, I want to limit my character's movement within the level boundary, which is a plane. I don't want him to go beyond the limit and fall from there, I just want him to be stopped there by an invisible wall. Is there anyone who can help me with this problem ? Thank you in advance !
Answer by Max 4 · May 24, 2011 at 06:52 AM
There are a few ways to do this. Firstly, you could add a box collider to an empty game object, and stretch that however you like. Then there are more complicated ones, like this:
transform.position.x=Mathf.Clamp(transform.position.x,0, 100);
transform.position.z=Mathf.Clamp(transform.position.z,0, 100);
There are many more ways in between, as well however.
Is there any way which is considered better than others ? If not, I think I will just stick with the method which creates "invisible walls" since it seems to be the simplest way to me. Thank you very much !
can anyone tell me when i use this code on the player when i reach those restrictions the camera shakes as if the player tries to go past that limit.
part of the player's movement code is like this:
var y$$anonymous$$ove : float = Input.GetAxis("Vertical") Time.deltaTime PlayerSpeed; transform.Translate(Vector3(0,y$$anonymous$$ove,0));
just basic code so anyone knows why this happens?
@leonalchemist Does the player have a rigidbody? If so, Click freeze rotation in all axes.