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
1
Question by Jason Hamilton · Sep 06, 2010 at 06:30 AM · javascripttriggerontriggerenter

What is the easiest way to make an object notice another?

Hey, I've been trying to make a TD game and one big problem I've run into is getting the tower to notice the enemies, which is easier? RayCast or OnTriggerEnter?

heres the script I've been working on. but it doesn't work. its applied to a trigger sphere which has the towers top part as its child.

var myEnemy;

function OnTriggerEnter (enter : Collider) { if(enter.gameObject.tag == "Enemy") { myEnemy = enter.transform; } }

function Update() { transform.LookAt(myEnemy.position); }

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by jtbentley · Sep 07, 2010 at 01:29 PM

Well that should work... Except I'd specify the variable at the top.. Just because it's a good habit to have. I would probably also have a boolean so the tower doesn't look unless a target is acquired.

var hasTarget = false; var myEnemy : Transform; private var myTransform : Transform; // Cache the transform to reduce slow get-component calls

function Start() { myTransform = transform; }

function OnTriggerEnter (enter : Collider) { if(enter.gameObject.tag == "Enemy" && !hasTarget) // Don't want to acquire a target if we still have one { myEnemy = enter.transform; hasTarget = true; } }

function Update() { if (hasTarget) myTransform.LookAt(myEnemy.position); }

Also worthy of note - both objects need colliders for this to work, and at least one has to be set to trigger :)

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 Loius · Oct 20, 2010 at 06:25 AM 0
Share

An additional note, a moving object won't reliably trigger OnBlah collision events unless it has a rigidbody - so add a Rigidbody to your enemies. You can set it to is$$anonymous$$inematic to continue controlling them through script, too.

avatar image
0

Answer by AliAzin · Sep 06, 2010 at 06:55 AM

OnTriggerEnter is easier to use and has much better performance.

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
0

Answer by james flowerdew · Sep 09, 2010 at 12:25 PM

Could you ompare the transform.positions and use distance? If you can, this is very cheap and effective.

if((transform.position-vBadObject.transform.position).magnitude<myAlertDistance){
StartFiring();
}

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

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

Check collision of childs Trigger in Parents script [JS] 1 Answer

Cueing an Animation From Another GameObject's Script (Javascript) 2 Answers

Rigidbody + Multiple Trigger Colliders - OnTriggerEnter - Get which Collider triggered the event issue 1 Answer

C# - Problem with trigger that won't activate 1 Answer

[Solved] Exit scene OnCollisionEnter not working 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