Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Sphere, internal collision 1 Answer

How do i get world position of collider if it inside multiple scaled gameobjects? 0 Answers

Physics2D.OverlapCircleAll returning colliders outside of the area 1 Answer

GameObject1 move away when GameObject2 gets close 1 Answer

Sphere with collider entering the flat surface 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges