• 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 JackieKan · Dec 05, 2013 at 01:33 PM · 2djavascriptcollision

How to make it so enemies only move towards the player when the player is colliding with an object

It's more so when you touch an object with the tag "light" then any object with the tag "enemy" will become active and move towards the player.

 #pragma strict
 
 function Start () 
 {
     GetComponent("ENEMY").active = false;
 }
 
 function Update () 
 {
 
 }
 function OnCollisionEnter (collision : Collision)
 {
     while(collision.gameObject.tag == "light")
     {
         GetComponent("ENEMY").active = true; 
     }
     GetComponent("ENEMY").active = false;
 }


[1]: https://www.dropbox.com/s/cc925rfeclh444a/Screenshot%202013-12-05%2008.26.30.png

[2]: https://www.dropbox.com/s/cc925rfeclh444a/Screenshot%202013-12-05%2008.26.30.png

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
0

Answer by stevethorne · Dec 05, 2013 at 03:16 PM

First, you don't want to use a 'while' statement there. That will loop forever if the object is a 'light' object. Replace that 'while' with 'if' and get rid of the line 'GetComponent("ENEMY").active = false;'.

Second, you will need to have an OnCollisionExit function that disables the Enemies if you're leaving the light object. It will be the same as your OnCollisionEnter function, but you will set the active state to false.

Comment
Add comment · Show 15 · 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 JackieKan · Dec 05, 2013 at 06:46 PM 0
Share

Like this? I forgot to mention I'm trying to make everything with the tag "enemy" active/inactive depending on the collision with "light".

 function Start () 
 {
     GetComponent("ENE$$anonymous$$Y").active = false;
 }
 
 function Update () 
 {
 
 }
 function OnCollisionEnter (collision : Collision)
 {
     if(collision.gameObject.tag == "light")
     {
         GetComponent("ENE$$anonymous$$Y").active = true; 
     }
 }
 function OnCollisionExit (collision : Collision)
 {
     if(collision.gameObject.tag == "light")
     {
         GetComponent("ENE$$anonymous$$Y").active = false; 
     }
 }
avatar image stevethorne · Dec 05, 2013 at 07:36 PM 0
Share

@Jackie$$anonymous$$an That looks about right, try that out and see if it does what you need it to.

avatar image JackieKan · Dec 06, 2013 at 01:46 PM 0
Share

I put it on the Player but nothing happened, no errors at least.

avatar image stevethorne · Dec 06, 2013 at 01:56 PM 0
Share

Are the light objects you're colliding with marked as Trigger colliders? If so you'll need to use the functions OnTriggerEnter and OnTriggerExit.

avatar image JackieKan · Dec 06, 2013 at 04:27 PM 0
Share

I tried making the light objects triggers:

 #pragma strict
 
 function Start () 
 {
     GetComponent(gameObject.tag == "enemy").active = false;
 }
 
 function Update () 
 {
 
 }
 function OnTriggerEnter (collision : Collision)
 {
     if(collision.gameObject.tag == "light")
     {
         GetComponent(gameObject.tag == "enemy").active = true; 
     }
 }
 function OnTriggerExit (collision : Collision)
 {
     if(collision.gameObject.tag == "light")
     {
         GetComponent(gameObject.tag == "enemy").active = false; 
     }
 }

Nothing happens, the enemy still starts activated and doesn't deactivate

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

17 People are following this question.

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

Related Questions

How to make it so enemies only move towards the player when the player is colliding with an object 1 Answer

Collision problem 0 Answers

Creating a draggable object - and defining areas. 1 Answer

Disabling the Z axis? 1 Answer

Collision Detection in Capsule Collider 0 Answers

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