- Home /
Shoot script not working anymore....
So I messed with my Bullet script so that it would instantiate an explosion prefab when it collided with something/ after 5 seconds if it didn't... and somehow now ONLY the explosion instantiates and not the bullet itself, when I fire the bullet doesn't move anymore and so just explodes on itself. If anyone could tell me what I did wrong that would be really appreciated.
Here is the Shooting script sent on an empty character where the bullet should spawn:
public class Shoot : MonoBehaviour
{
public Rigidbody2D Bullet;
public float speed = 20f;
private PlayerControl playerCtrl;
private Animator anim;
void Awake()
{
anim = transform.root.gameObject.GetComponent<Animator>();
playerCtrl = transform.root.GetComponent<PlayerControl>();
}
void Update ()
{
// If the fire button is pressed...
if(Input.GetButtonDown("Fire1"))
{
anim.SetTrigger("Shoot");
// If the player is facing right...
if(playerCtrl.facingRight)
{
// ... instantiate the bullet facing right and set it's velocity to the right.
Rigidbody2D bulletInstance = Instantiate(Bullet, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
bulletInstance.velocity = new Vector2(speed, 0);
}
else
{
// Otherwise instantiate the bullet facing left and set it's velocity to the left.
Rigidbody2D bulletInstance = Instantiate(Bullet, transform.position, Quaternion.Euler(new Vector3(0,0,180f))) as Rigidbody2D;
bulletInstance.velocity = new Vector2(-speed, 0);
}
}
}
}
And here is the Bullet script itself with the Explosion prefab
public class Bullet : MonoBehaviour
{
public GameObject Explosion; // Prefab of explosion effect.
void Start ()
{
Destroy(gameObject, 5);
}
void OnExplode()
{
// Create a quaternion with a random rotation in the z-axis.
Quaternion randomRotation = Quaternion.Euler(0f, 0f, Random.Range(0f, 360f));
.
Instantiate(Explosion, transform.position, randomRotation);
}
void OnTriggerEnter2D (Collider2D col)
{
// If it hits an enemy...
if(col.tag == "Enemy")
{
// Enemy script and call the Hurt function.
col.gameObject.GetComponent<Enemy>().Hurt();
OnExplode();
Destroy (gameObject);
}
{
OnExplode();
Destroy (gameObject);
}
}
}
Answer by KalleH · Apr 11, 2015 at 03:03 PM
Probably your bullet hits some collider like the player itself while instantiated which causes the explosion to be instantiated but at the same time the bullet itself is destroyed (lines 37-41). To fix this you could for example create a layer for objects that you want the bullet to collide with and then in OnTriggerEnter2D check if the other collider has this layer.
Answer by DoTA_KAMIKADzE · Apr 11, 2015 at 12:00 PM
I guess your second script lines #37-41 get called on spawn, because probably your "transform.position" in your first script is a player position I guess and your bullet instantly hits your player on spawn. To verify my guess add this Debug line:
{
Debug.Log(col.name);//this should tell you what is being hit, most likely player
OnExplode();
Destroy (gameObject);
}
In order to overcome this, you have a lot of options, I'll just write here few easiest ones over here:
1)The simplest one:
if(col.tag != "Player")//or check by player name like I did in Debug or whatever else specific to player
{
OnExplode();
Destroy (gameObject);
}
2.1)The way I DO NOT recommend - add a "spawn timer" that will count few seconds before it gets out of player and only then be able to hit something.
2.2.)Second way that is more or less OK - same "spawn timer" or TriggerExit that will be affected only by player, if anything else is hit meanwhile then hit it.
3)Spawn outside of the player, add an empty object as a child to player and call it spawn position and then just instantiate bullet transform from its transform.
Your answer
Follow this Question
Related Questions
Bullet stops when new bullet instantiates pls help 3 Answers
Character has free movement when 'shooting' 0 Answers
How to to not move when shooting? 0 Answers
Problem with shooting an object in 2D 1 Answer
why wont my fire point flip? 0 Answers