- Home /
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.
I have no idea what I can do to fix this, so if you have any ideas, I'm all open. Thanks!
Answer by meat5000 · Nov 28, 2014 at 09:19 PM
Hack fix, after it's collided once, change it's tag.
THAN$$anonymous$$ 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";
}
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.
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
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Fist Punch Collision 0 Answers
Using raycast for visible bullet collision (not oncollisionenter) in C# 1 Answer