Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by projectInxomplete · Jun 22, 2011 at 09:36 AM · enemydisabledeactivateproximity

How to activate / deactivate an enemy on player proximity

I have no idea how to activate / deactivate enemies when the players comes close / moves out of range (so they don't cost cpu cicles). The only thing I can think if is calculation the position from the enemy to the player ondate, but if you put 100 enemies in the game that would mean 100 distance calculation per update. There must be a more effective way to do this, because almost all games would need something like this to keep it fast.

So is there a way to totaly disable an object? And how do i do this using player proximity.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Ashkan_gc · Jun 22, 2011 at 01:10 PM

you can use a trigger around your enemy. attach a trigger (a big one) to a gameObject you make it the children of your enemy. then attach a code like this to it.

 function OnTriggerEnter (other : GameObject)
 {
 if(other.tag == "Player") transform.parent.enabled = true;
 }
 function onTriggerExit(other : GameObject)
 {
 if(other.tag == "Player") transform.parent.enabled = false;
 ]

the code assumes that the player has a player tag assigned to it but you can identify the player in any other way. the transform.parent.enabled also enables/disables the parent of the object but you can declare a public variable and attach the enemy to it in the editor if you want. also you might want to enable/disable a script of the enemy so you can declare the variable of type "thatScript". where thatScript is the name of your script.

 var enemy : EnemyAI;

and then enable disable the enemyAI script of the object attached to this variable in editor or using GameObject.Find and GetComponent.

in this way using triggers you will not call the code in update and they are really cheap even on ios triggers don't take much. also you can make big triggers for each reagon to do enable/disable on a per reagon basis.

Comment
Add comment · Show 1 · 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 projectInxomplete · Jun 22, 2011 at 07:43 PM 0
Share

Why do you do "transform.parent.enabled = false", isn't disabling the scripts enough? What does it do?

avatar image
1

Answer by Eric5h5 · Jun 22, 2011 at 09:45 AM

You wouldn't have to do it per update; you could use coroutines or InvokeRepeating to do it 10 times per second or something. It's probably better if you use large sphere triggers for the enemies, though. Then you can do

 function OnTriggerEnter () {
     enabled = true;
 }
 
 function OnTriggerExit () {
     enabled = false;
 }
Comment
Add comment · 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
1

Answer by projectInxomplete · Jun 22, 2011 at 07:41 PM

Thanks, I've taken the trigger aproach, I've created a script which I attached directly to the enemy. My final (and working) code:

 private var AIScript:EnemyAI;
 
 function Awake() {
     var sc : SphereCollider;
     sc = this.gameObject.AddComponent ("SphereCollider");
     sc.radius = 50;
     sc.isTrigger = true;
     
     AIScript = GetComponent(EnemyAI);
     AIScript.enabled = false;
 }
 
 function OnTriggerEnter(other:Collider){
     if(other.tag == "Player") AIScript.enabled = true;
 }
 
 function OnTriggerExit(other:Collider){
     if(other.tag == "Player") AIScript.enabled = false;
 }
Comment
Add comment · 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
1

Answer by ahsen35813 · Apr 02, 2021 at 11:17 PM

You could use a script like this:

 float activationDistance = 5f;
 GameObject enemy;
 GameObject player;
 void Update(){
 if ((enemy.transform.position - player.transform.position).magnitude < activationDistance)
 {
     enemy.enabled = true;
 }
 else
 {
     enemy.enabled = false;
 }
 }

Put this script on a parent object with the enemy as the child object because a disabled gameObject can't re-enable itself. Sorry for the bad code formatting, but it should work. Pretty reliable and should work even if the enemy travels too fast to cause problems with colliders and things, since it's based on just the enemy's distance from the player. In my opinion, this is a more reliable system and a bit simpler too.

Comment
Add comment · Show 1 · 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 ms11153 · Apr 03, 2021 at 08:39 PM 0
Share

I tried this but the character keeps flickering.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to setActive(true) then false on enemy child gameobject based on closest enemy to player? (Closest Enemy Target System) 0 Answers

enemy waypoints and detection scipting 2 Answers

Is there any command that can reference deactivated objects? 1 Answer

Expecting : found ; error 1 Answer

Enemy follows player then attacks! 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