- Home /
How to use Sphere colliders to detect the "Player"
can someone break this down for me and explain this further?
Use triggers. A simple sphere trigger can work wonders though. You get OnTriggerEnter/Exit calls when exiting the sphere of influence you want
function OnTriggerEnter (c : Collider) { if (c.CompareTag("Player")) enabled = true; }
function OnTriggerExit (c : Collider) { if (c.CompareTag("Player")) enabled = false; }
Everytime i try and this script in, iget a lot of errors and i must be doing something wrong.
Answer by Sebas · Feb 24, 2010 at 10:17 PM
If you attach this kind of script to an object with a sphere collider (set to IsTrigger), every time an object which is tagged as "Player" enters the trigger, something is enabled. So far the logic is sound. However, you would have to declare your variable "enabled" at some point (beginning of the script: var enabled : boolean) and also you are missing some {} for your if statements. Try something like this:
var turretEnabled : boolean;
function OnTriggerEnter (c : Collider) { if (c.CompareTag("Player")) { turretEnabled = true; } }
function OnTriggerExit (c : Collider) { if (c.CompareTag("Player")) { turretEnabled = false; } }
Should i add this into the update function?? If not here should i place it? And why are using the Variable called Boolean??
The above code needs to be copied into an empty js script. No update function or anything else is needed. The OnTriggerEnter/OnTriggerExit are functions on their own and they get called when something enters the object's triggerzone (collider set to isTrigger). enabled is a boolean-type variable because it can only hold two values/states, either true or false (as can be seen within the functions). 'Enabled' is either set to true or false. You call such a variable a boolean.
I'm getting the ERROR
Node "Enabled" has not been correctly processed And points to the line enabled = true;
hmmm $$anonymous$$aybe i should explain the object and such a little more. I have a 3 piece object that rotates along its pivot looking for my player. Where it will then instantiate a projectile. I want to add a script or something causing it to only shoot when the player reaches within its range.
or if possible have the turret offline until it reaches the Sphere collider trigger.
Thanx
my bad, enabled is no good choice for a variable name. I changed it to turretEnabled. 'enabled' already denotes the state of a component (being enabled or not) and as such is a no-go for a variable name.
It fixed the error,
BUT, no matter where i add this script in i cannot get any results. $$anonymous$$y enemy turret will none stop fire at me even if i'm not within its sphere collider range.