- Home /
Fired projectile collision with player issue
Currently I have a bullet projectile coming from a space ship mesh that is surrounded by a sphere(shield) collider that is larger than the ship.
The problem is that my projectiles for the weapons are fired from inside this collider so they immediately make contact and apply damage to the player.
I could use layer based collision management but I intend to use the same weapons and ammo systems for NPC's so I need them to be able to collide with the player and cause damage as usual. If possible I'd like to avoid creating 'NPC Only Projectiles' and 'Player Only projectiles'.
Is there another method to handle this? This problem will be universal since most of the enemies will have a shield type system. I haven't worked with the collision system much and can't think of any quick fixes or work around.
Answer by Bovine · Jul 20, 2012 at 09:34 PM
Personally I'd use layers and just set the layer when you instantiate the prefab for you projectile.
Alternatively you could have a script on your projectile that would identify who fired it, then in your OnCollisionEnter() you could get this script from the projectile and see who's 'bullet' it is.
i.e.
void OnCollisionEnter(Collision col)
{
MyBulletInfo bullet = col.GameObject.GetComponent<MyBullletInfo>();
if(bullet == null) return;
// does it match?
if(bullet.Source == this.Source)
{ // handle the hit
}
}
Where Source is some type that would identify the player/npc, i.e.:
public enum ObjectOwner
{
Player,
Enemy
}
Naturally this could just be a bool, or alternatively, you could make Source an ID and give players and enemies a unique ID (i.e. a unique int) and then bullets would not hit shields they were aligned with, but could hit other shields, including enemy shields.
But I imagine somewhere you are doing:
GameObject bullet = GameObject.Instantiate(MyActiveBullet) as GameObject;
In which case you surely can add another line:
bullet.layer = wasFiredByPlayer ? LayerConstants.PlayerBullets : LayerConstants.EnemyBullets;
Or similar. This is much more efficient and there is no specialisation of your missile prefabs, you just need to know whether the player or an enemy fired a weapon and I imagine you will know that.
Naturally, you can then adjust the physics settings to determine which bullets can collide which which layers.
Presumably, this is the approach you were considering, perhaps it is made easier seeing how easy it would be to place the 'bullets' on the appropriate layer at runtime?
Hope that helps!
That method is probably a better way to do it but what I ended up doing (at least for now) is making it spawn and immediately do a collision event on exit.
So basically it spawns, gets applied speed, realizes that its inside a collider, waits until it exits and then begins its normal behavior. Friendly fire is not possible with this method but this is single player only so I'm not worried about it. I'm using Play$$anonymous$$aker so this is pretty easy to do with it and seems to work fine for the time being.
Thanks for your detailed response! I'll be making use of it when I get the prototype system done and need to get more technical.
Your answer
Follow this Question
Related Questions
Projectile/Shield Collisions with Friendly Projectiles Passing Through 1 Answer
Collision occurring when it should not! Help! 1 Answer
Projectiles, their speed, and collisions 1 Answer
How can i make a projectile attack travel through multiple enemies dealing damage to all of them? 1 Answer
Generated Terrain not colliding! HALP. 0 Answers