Hey guys!
I’m new in Unity and am watching some videos to learn this engine. It’s not hard, and I’m already doing some nice stuff.
But I’m breaking my head over here.I made an example so you can see it.
Please take a look in this picture.
That cilinder whith this cube “hat” is my hero. And he hold a weapon with 10 ammo (as you can see in the screen.
That capsule in the right give the weapon 3 ammo if the hero collide it. Everything OK until them. But when I collide…
See? There’s a “double collision”. So the ammo received +6 and not +3;
Here’s the console. You can see he “hit” twice.
Here’s my Recharge script. (Which is in the capsule)
#pragma strict
function Start () {
}
function Update () {
}
function OnCollisionEnter(collision : Collision) {
if(collision.gameObject.tag == "Hero") {
Destroy(gameObject);
weapon.contAmmo = arma.contAmmo + 3;
print("hit");
}
}
Here’s the weapon script
#pragma strict
var bullet: GameObject;
var ammo: UnityEngine.UI.Text;
public static var contAmmo : int;
function Start () {
contAmmo = 10;
}
function Update () {
ammo.text = "Ammo: " + contAmmo;
if(Input.GetKeyDown("space")) {
if(contAmmo > 0) {
Instantiate(bullet,transform.position,transform.rotation);
contAmmo = contAmmo - 1;
}
}
}
I know I can solve this putting a flag on the collision detection like this:
if(collision.gameObject.tag == "Hero") {
if (!used) {
used= true;
Destroy(gameObject);
weapon.contAmmo = weapon.contAmmo + 3;
}
}
But I just want to know why this is happening
Thanks for reading!