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 timo0060 · Jul 09, 2012 at 02:03 AM · javascripttriggerenemybullet

On TriggerEnter issues

Hello, so I've just started to make my 2D space shooter game and I'm working on shooting the enemies. Such an easy task I know, but I don't understand where I'm going wrong with it. Here's my code attached to my bullet prefab (bullet is a trigger) that deals with triggers:

 function OnTriggerEnter(otherObject: Collider)
 { 
    if(otherObject.gameObject.tag == "Enemy")
  {
  print("Hit");
  Destroy(gameObject);
  otherObject.GetComponent(EnemyScript).life -= 1;
  }
 }

I've made sure my Enemy is tagged as "Enemy", and it is a trigger. I'm not sure what's wrong with this, but it probably just needs a new pair of eyes. Also, I have the "print()" there to tell me if there is collision. Thank you for all your help.

Comment
Add comment · Show 3
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 OperationDogBird · Jul 09, 2012 at 02:10 AM 1
Share

How fast is the bullet going? It may be passed the collider the next time it updates the trigger events. If thats the problem you can always use a Raycast forward 1 gameUnit(or to ur scale) to detect events that may get overlooked.

Other things here...When you use Destroy() it should be called last(since you destroy this object thats running the code) therefore the EnemyScript.life.... may never be called since you destroy the script the line before. Also the condition can be cleaned up as such

 if(otherObject.CompareTag("Enemy")) //run code

tag is also a member of collider so you could do away with gameObject portion of the condition.

avatar image timo0060 · Jul 09, 2012 at 02:13 AM 0
Share

Your comment gave me an idea of putting a Rigidbody onto the bullet, fortunately, that was the solution. Thank you very much for your comment, it helped a lot.

avatar image OperationDogBird · Jul 09, 2012 at 02:14 AM 1
Share

Oh, good deal. Glad you got some inspiration from that. Cheers

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by timo0060 · Jul 09, 2012 at 02:14 AM

I just had to add a Rigidbody onto my bullet. I knew it was something simple that I overlooked.

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

6 People are following this question.

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

Related Questions

destroy a non-trigger object hitting a trigger object? 1 Answer

What’s wrong with my Safe Volume (safe zone) code? 0 Answers

Collsions problem 0 Answers

bullets kill all enemies, not just the ones hit. 1 Answer

Need help on modifying Enemy AI script. 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