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 Essential · May 06, 2012 at 02:51 AM · ontriggerenterbullet

OnTriggerEnter for moving object

I've attached an OnTriggerEnter event to my bullets, trying to get the bullet to destroy itself when it encounters an object. But it doesn't seem to work on static objects like walls.

I could use OnCollisionEnter but that's considerably more processor heavy and I don't need any collision information…

I could add an OnTriggerEnter script to all objects in the scene, but also seems wasteful…

Comment
Add comment · Show 5
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 kolban · May 06, 2012 at 03:29 AM 0
Share

Does your "bullet" game object also have a RigidBody associated with it?

avatar image Essential · May 06, 2012 at 03:35 AM 0
Share

Yeah it does. It's kinematic.

avatar image DracoScorpius · May 06, 2012 at 04:33 AM 1
Share

I'm not an expert, but a trigger will only be enacted when it hits a trigger, so if you're going to do this, then the objects must be triggers as well. You don't have to add a script though, just simply tag them as something, label them as trigger, then have the bullets get destroyed when they hit that tag. But doing this could make your character not collide, so your best bet would be to have it do a normal collision i believe.

avatar image kolban · May 06, 2012 at 04:37 AM 1
Share

I'm pretty new here ... but one thought ... it might be that a trigger is initiated when something "enters" the trigger collider and not when your dynamic object enters another trigger space.

avatar image Essential · May 06, 2012 at 07:15 PM 0
Share

I believe you're both right. I'll take Bérenger's approach

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Berenger · May 06, 2012 at 05:58 AM

You should cast a ray when the bullet is shot to find it's end point. Once reached, it's destroyed. The speed of a bullet make it inapropriate for colliders, as it travels several times its size in one frame.

Comment
Add comment · Show 2 · 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 Essential · May 06, 2012 at 08:56 PM 0
Share

On second thoughts, I don't understand how this can work… What if an enemy moves in front of the bullet before it hits the wall? I would have already calculated the raycast destination…

Bullets in my game are not instantaneous. They are visible (like an old skool shooter)

avatar image Berenger · May 06, 2012 at 09:23 PM 0
Share

$$anonymous$$ind of like the rockets in $$anonymous$$ario ? In that case, you could cast a ray every X seconds with a coroutine or InvokeRepeating.

As for the trigger, as far as I know the entre event is raised when a rigidbody enter the trigger, not the other way around. So a moving target would raise it, but not a static one.

avatar image
0

Answer by GC1983 · May 06, 2012 at 05:50 AM

OnTriggerEnter will work fine. Tag whatever you are shooting at something like Enemy or Target and attach the script the bullet itself.

 function OnTriggerEnter(collider : Collision)

{ if(collider.gameObject.tag = "Enemy") { Destroy(this.gameObject); } }

That should be it.

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 Rrrush5000 · Aug 01, 2017 at 04:08 PM

this is C# and this works for me

using UnityEngine; using System.Collections;

public class Mover : MonoBehaviour { public float speed; public int damage = 5;

 void Start ()
 {
     GetComponent<Rigidbody2D>().velocity = transform.right * speed;
 }

 void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.collider.tag == "Enemy") 
     {
         collision.collider.gameObject.GetComponent<Enemy>().Damage (damage);
         Destroy (gameObject);        
     }

     if(collision.collider.gameObject)
     {
         Destroy (gameObject);
     }

 }

}

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

9 People are following this question.

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

Related Questions

Could some one help me with this script... 5 Answers

Objects do not destroy on Collision 1 Answer

Piercing bouncing projectile 1 Answer

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

Enemy with character controller has inconsistent hit detection 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