- Home /
Shooting with Gun
Hello, I have a big problem on my FPS :
When I use Mouse0, a bullet appear and have a velocity, but I would like create damages to ennemi when the bullet is trigger with it.
I do this but I got damages and the other player not.
Thanks, Excuse me for my bad English.
(The game is Multiplayer).
Answer by Ambrose998800 · Sep 28, 2015 at 01:33 PM
Seems to be a problem with your own collider.
Look for 'Layer Collision Matrix' in Edit -> Project Settings -> Physiks. Make sure, your bullet ignores your own player collider by using different layers.
If using the collision matrix. Enemies can't use the same bullet prefab. 2nd Prefab with a diff layer that can hit the player.
Or when Instantiating the prefab ignore the collision http://docs.unity3d.com/ScriptReference/Physics.IgnoreCollision.html
// Instantiate a bullet and make it ignore collisions with this object.
var bulletPrefab : Transform;
function Start () {
var bullet = Instantiate(bulletPrefab) as Transform;
Physics.IgnoreCollision(bullet.GetComponent.<Collider>(), GetComponent.<Collider>());
}
It doesn't work :/ I have always the same problem, the player dead with his own bullets and his bullets don't do damage to my ennemi :/
I use both. Both ways work. Post your code and let us have a look.
Answer by Dark_Seth · Oct 12, 2015 at 03:34 PM
Ok. Didn't find any Ignore statements inside. Added this one for you. Just know if gun has the collider and the gun is where the script is attached it will ignore collision with the gun. Might need to add the parent in too.
public IEnumerator OneShot ()
{
//GetComponent<AudioSource>().PlayOneShot(gun.sound);
canShoot = false;
gun.bulletsInAmmo --;
float x, y;
if (!GetComponent<Crosshair>().isScope)
x = Random.Range(-(100-(float)GetComponent<Crosshair>().tirAuJugee)/5000, (100-(float)GetComponent<Crosshair>().tirAuJugee)/5000);
else
x = Random.Range(-(100-(float)gun.accuracy)/10000, (100-(float)GetComponent<Crosshair>().tirAuJugee)/10000);
if (!GetComponent<Crosshair>().isScope)
y = Random.Range(-(100-(float)GetComponent<Crosshair>().tirAuJugee)/5000, (100-(float)GetComponent<Crosshair>().tirAuJugee)/5000);
else
y = Random.Range(-(100-(float)gun.accuracy)/10000, (100-(float)GetComponent<Crosshair>().tirAuJugee)/10000);
Rigidbody instantiatedProjectile = Network.Instantiate(projectile, transform.position + new Vector3(0, 0, 0), transform.rotation, 0) as Rigidbody;
instantiatedProjectile.velocity = GetComponent<Transform>().TransformDirection((Vector3.forward + new Vector3(x, y, 0)) * gun.velocity + new Vector3(0, 0, 0));
//Added
Physics.IgnoreCollision(instantiatedProjectile.GetComponent.<Collider>(), GetComponent.<Collider>());
GetComponent<RotateX> ().rotationX = Mathf.Lerp (GetComponent<RotateX> ().rotationX, GetComponent<RotateX> ().rotationX - 0.6f, 1f);
yield return new WaitForSeconds (gun.rateOfFire);
canShoot = true;
}
Thank you for all your post, but I already add the IgnoreCollision, in a singleplayer server, my player shoot and he doesn't got damages :)
But when I have two players in a server, if one of two shoot, he dead with his own bullets :/
Your answer