• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Whatee · Feb 24, 2010 at 09:48 PM · colliderdistancesphererange

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Whatee · Feb 25, 2010 at 05:27 AM 0
Share

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

avatar image Sebas · Feb 25, 2010 at 05:36 AM 0
Share

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.

avatar image Whatee · Feb 25, 2010 at 06:00 AM 0
Share

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

avatar image Sebas · Feb 25, 2010 at 06:08 AM 0
Share

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.

avatar image Whatee · Feb 25, 2010 at 06:29 AM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

GameObject1 move away when GameObject2 gets close 1 Answer

SphereCastAll doesn't works 1 Answer

Activate physics when animation ends 0 Answers

Material Sphere Green 1 Answer

Repeat While Object in Range 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges