Disabling a button when entering a trigger?

Long story short, how do you create a trigger area so a player’s button is disabled whilst in that trigger?

Short story long -

So here’s the deal…

I’m making a shooter akin to Space Harrier and I have a player + camera moving forward on a terrain at a set speed using the animation feature(Picture below). Whilst moving forward at this set speed the player (The white Cube) can moved up/down/left/right using this code…

var moveSpeed : float = 5;
function Update () {
var h : float = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed; 
var v : float = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed; 
transform.Translate(h, v, 0);
}

…I didn’t want the player to be able to move off the side of the screen so I assumed I could create walls, turn their renders off and have them move along with the player at the edges of the screen providing invisible barriers…However I think due to the forced moment forward, despite trying to restrict the rigid body on the players cube, the physics cause to the player to fly all over the place.

My thinking is I can remove the physics from the wall, turn them into triggers and have a code so it’ll disable the players button to move in that direction so for example,

player moves to the left side of the
screen,
they enter the trigger area,
button for going left turns off,
player can only move right/up/down,

And for the opposite for the other side…

player moves to the right side of the
screen,
they enter the trigger area,
button for going right turns off,
player can only move left/up/down,


My first thought is a boolean script (if Player-in-trigger is true, turn off left input) that way I’ll get the effect of a invisible barrier without interfering with the physics…

What code would you recommend? can anyone think of a better way round this?

Thanks for reading!

Mhh… I haven’t fully understood why you want a trigger area, but what about just testing the players position in the Update method and just plain not set him when he is too much sideways?

function Update () {
    var h : float = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
    var v : float = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
    // the numbers -10 and 10 are just arbitrary values. You want to test out what works for you
    if (transform.position.x + h > -23 && transform.position.x + h < 23 &&
        transform.position.y + v > -11 && transform.position.y + v < 11)
            transform.Translate(h, v, 0);
}

I got the effect needed from using Clamps -

var moveSpeed : float = 5;
function Update () {
    var h : float = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
    var v : float = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
  
       var pos : Vector3 = transform.position;
    pos.x = Mathf.Clamp(pos.x + h, -22, 22);
    pos.y = Mathf.Clamp(pos.y + v, 6, 28);
    transform.position = pos;
       
        }