how can I tell the compiler that when the character reaches a certain point on the screen can not go beyond even if you press the input? I just thought of putting a block invisible but wanted to do it through code
Something like this?
if(Camera.main.WorldToScreenPoint(player.transform.position).x > certainPointOnScreen.x) // Assuming point is a Vector3
{
// Stop Input and do something
}
else
{
if(Input.GetAxis("Horizontal") != 0)
{
// Input
}
}
Another way is to set some invisible colliders to prevent the player from moving beyond the colliders.
invisible colliders are just empty gameObjects with collider components attached. You can add in unity’s generic boxes and remove the mesh components if that makes more sense to you (same end result, gameObject with just a collider and transform component)
what I do not understand is what is the function to block the movement. I was thinking of doing so:
if (player.transform.position.x <= 10) {
/ / How do I block the movement?
}
You would only check input if the player’s position is within the allowed space. If the player has hit the boundary wall, you should nudge the player a little to put him back into the allowed space.
The most easiest way is to just set some colliders to act as invisible walls. If your player is not using physics for movement, you can do a collision check to detect if the player is attempting to leave the boundary then just do something to make the player not leave the boundary.
You could also use Mathf.Clamp to make sure the position is within the bounds you want.
Example From Script Reference:
function Update () {
transform.position = Vector3(Mathf.Clamp(Time.time, 1.0, 3.0), 0, 0);
}
Colliders will work, but if you want to do it purely in code you’d have to run a check before allowing input or just clamp it like above.
Example of Checking Before Allowing Motion:
function Update () {
if (transform.position.x < 10) {
// put move code here
}
}
Clamping is good because it will make sure the value you want to clamp is within the min and max that you set. You can use both, but you don’t have to. An upside to using both is that you can’t accidentally move out of the boundaries you’ve set (depending on your code, the checking example I have can experience that problem) and be unable to get back, so try different things to get the functionality you want. ![]()
Havent tried it, should work fine as expected:
var cam : Transform;
function Update()
{
if(transform.position.x < 10)
{
cam.GetComponent("CharacterMotor").enabled = false;
}
else
{
//Same there but with .enabled = true;
}
}
OffCourse you can also replace CharacterMotor with some other player component such as FPSInputController, or whatever other.