x


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.

more ▼

asked Feb 24 '10 at 09:48 PM

Whatee gravatar image

Whatee
-4 2 3 4

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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; 
    }
} 
more ▼

answered Feb 24 '10 at 10:17 PM

Sebas gravatar image

Sebas
4k 12 18 45

Should i add this into the update function?? If not here should i place it? And why are using the Variable called Boolean??

Feb 25 '10 at 05:27 AM Whatee

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.

Feb 25 '10 at 05:36 AM Sebas

I'm getting the ERROR

Node "Enabled" has not been correctly processed And points to the line enabled = true;

hmmm Maybe 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

Feb 25 '10 at 06:00 AM Whatee

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.

Feb 25 '10 at 06:08 AM Sebas

It fixed the error,

BUT, no matter where i add this script in i cannot get any results. My enemy turret will none stop fire at me even if i'm not within its sphere collider range.

Feb 25 '10 at 06:29 AM Whatee
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1704
x355
x199
x106

asked: Feb 24 '10 at 09:48 PM

Seen: 2731 times

Last Updated: Feb 24 '10 at 09:48 PM