- Home /
constricting movement to cubes via collider-trigger-boolean scripting confusion
i'm trying to set up a script that will turn booleans on or off depending on triggers, in this example i'd like the red block to only be able to move to the edge of the green cubes and no further.
here's where i' m mostly failing at the moment: if i add an OnTriggerExit function to turn the boolean off it'll not be on even if the space it moves to is of the same type (i suppose because it's calling Exit on that trigger, which is fair enough), i've tried a few things and lots of head-scratching.
i've set up 4 colliders surrounding the player piece (which only moves 4 directions) and set them to trigger (or more like tried to) whenever they collide with various tagged objects in hope that it'd be straight forward to set up the movement script to ask if the boolean is on/off to allow movement in that direction (if north collider is colliding with floor, for example, movement north is possible).
here's one of the many mashed-up scripts i now have:
script
var target: Transform; // tagged "Block"
// var stone: Transform; // tagged "Block"
// var water: Transform;
var onfloor: boolean = false;
var inSpace: boolean = false;
/*function Start(){
if (target == null && GameObject.FindWithTag("Block"))
target = GameObject.FindWithTag("northTrigger").transform;
}*/
function OnTriggerEnter() {
if(target.gameObject.tag == "Block"){
onfloor = false;
collider.isTrigger = true;
print("north is grass");
collider.isTrigger = false;
}
if(target.gameObject.tag != "Block"){
onfloor = false;
collider.isTrigger = true;
print("north is grass");
collider.isTrigger = false;
}
}
/*
function OnTriggerExit() {
if(target.gameObject.tag == "Block"){
// collider.isTrigger = true;
ongrass = true;
print("north is nothing");
// collider.isTrigger = false;
}
}
*/
sory about the formatting there, can't even work out how to get that to print straight here, the "/" are supposed to be commented out sections incase you'd not gathered by now:/. as you may notice if you've bothered to read this far, i'm almost absolutely useless at this stuff:( no doubt there are myriad better and simpler solutions to this problem, i would like to hear of those that don't include rigidbodies too if you're prepared to write them... thanks also does anyone of a simple line-of-sight checking script i can rip that works[like this but maybe better:?
Format your code by indenting 4 spaces or 1 tab before pasting, or highlighting it and clicking the code button after pasting. Will fix it for you this time.
thanks, something useful i forgot to mention would be that the player piece here moves without deltaTime, probably more info would help to help:/
Answer by player2 · Jul 03, 2012 at 08:12 AM
so... incase anyone's interested i've created a most inelegant piece of script and eight colliders (two for each direction)to deal with this but now need to write them in somehow... in this example the collider scripts are called triggerNorthDown and triggerNorthUp, the booleans they switch are called NDownSafe and NUpSafe respectively and should both read safe to allow movement;) here's how i've butchered the movement script so far:
var roll:int=5;
var extpace:int=0;
var movespeed : int = 12;
function Start ()
{}
function Update ()
{
if (
gameObject.GetComponent(triggerNorthDown)NDownSafe == true &&
gameObject.GetComponent(triggerNorthUp)NUpSafe == true &&
roll+extpace>=0
&& Input.GetButtonDown("forward"))
{
transform.position.z += 1.0;
}
if (extpace+roll>=0 && Input.GetButtonDown("backward"))
{
transform.position.z += -1.0;
}
if (extpace+roll>=0 && Input.GetButtonDown("left"))
{
transform.position.x += -1.0;
}
if (extpace+roll>=0 && Input.GetButtonDown("right"))
{
transform.position.x += 1.0;
}
}
i'm sure it must be something like this:/...
Your answer
![](https://koobas.hobune.stream/wayback/20220613073542im_/https://answers.unity.com/themes/thub/images/avi.jpg)