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

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

How to add to a 3D object's length through code? 1 Answer

Object collision triggering null reference at runtime when colliding with clone of itself after being childed. 1 Answer

Collision for a 2d Top Down RPG using 2d Toolkit 1 Answer

Basics on 2d gameplay for a 2.5d Game 1 Answer


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