• 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
Question by Xysch · Nov 28, 2014 at 06:43 PM · c#collisionparticlesenemybullet

Stopping a bullet but letting the animation continue

Right now I have a major hiccup in my game. I have a code running so that a "Bullet" shoots from the whale and when it collides with something it stops.

This is exactly what I want, except when it stops, it still sits there for 3 seconds waiting for the particle system to finish. During those three seconds if another enemy collides with it, they also "die".

So I am not able to delete the bullet from the scene due to the particle system having to finish, and I need it to stop working when it collides. I've tried turning on and off the collider, but then it only worked sometimes.

Here's the code I'm using for the bullet:

 using UnityEngine;
 using System.Collections;
 
 public class MoveTrail : MonoBehaviour {
     
     public int moveSpeed = 230;
     public float killSpeed = 3f;
 
     void Start(){
         renderer.enabled = false;
         }
 
     // Update is called once per frame
     void Update () {
         transform.Translate (Vector3.right * Time.deltaTime * moveSpeed);
         Destroy (gameObject, killSpeed);
     }
 
     void OnTriggerEnter2D(Collider2D col){
         if (col.gameObject.tag == "Enemy") {
             particleSystem.emissionRate = 0;
             moveSpeed = 0;
         }
         if (col.gameObject.tag == "Sub") {
             particleSystem.emissionRate = 0;
             moveSpeed = 0;    
         }
     }
 }
 

And for the Enemies:

     void OnTriggerEnter2D(Collider2D col){
                 if (col.gameObject.tag == "Bullet") {
                         air = air - 10;
                 }
         }
 

I have a photo here to show you the problem. The bullet is hitting the sub as demonstrated by the red line, and then the bullet sits where the blue circle is, and the sub is constantly moving down, shown by the black arrow, so the enemies swim into the bullet and take damage as they follow the sub.

Bullet Problem

I have no idea what I can do to fix this, so if you have any ideas, I'm all open. Thanks!

unityproblem.jpg (67.6 kB)
Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by meat5000 · Nov 28, 2014 at 09:19 PM

Hack fix, after it's collided once, change it's tag.

Comment
Xysch

People who like this

1 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 Xysch · Nov 28, 2014 at 09:36 PM 1
Share

THANK YOU! And I had to add a little bit of code because otherwise it would only sometimes detect the collision for one code. So I added:

     IEnumerator killBullet(){
         yield return new WaitForSeconds (0.01f);
         gameObject.tag = "DeadBullet";
     }
avatar image

Answer by Foxtrek_64 · Nov 28, 2014 at 08:38 PM

What I would recommend doing is disabling the renderer for the bullet.

In the bullet script, below the line

public class MoveTrail : MonoBehaviour {

add private Renderer bulletRenderer;

Next, down in your bullet collide script, add this modification

 void OnTriggerEnter2D(Collider2D col){
          if (col.gameObject.tag == "Enemy") {
              particleSystem.emissionRate = 0;
              moveSpeed = 0;
              bulletRenderer.enabled = false;
          }
          if (col.gameObject.tag == "Sub") {
              particleSystem.emissionRate = 0;
              moveSpeed = 0;    
              bulletRenderer.enabled = false;
          }
      }


This should, if I'm reading your code correctly, make the bullet "disappear" and allow the particle effects to finish, then your code takes care of deleting the bullet.

Comment

People who like this

0 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 Xysch · Nov 28, 2014 at 09:10 PM 0
Share

The bullet is already not visible which I want, but I need it for the collider to stop working or something. The bullet stops and the enemies swim into the collider.

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

27 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Game 2x speed object pass through other 1 Answer

Object moving too fast, so that the collider does not work (C#) 2 Answers

Parent colliding bullet to the object 1 Answer

Collider Tag not working for enemy 3 Answers

My bullet has no velocity after it is spawned. 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