- Home /
How to make bullets pass through each other.
Hi, I am making a simple two player tank game similar to a game called tank trouble. The bullets are able to bounce off of other objects including copies of itself. How do I make it so that the bullets can go through each other, but still be able to bounce through other objects? This is my script:
using UnityEngine;
using System.Collections;
public class BulletScript : MonoBehaviour {
public int range = 200;
private GameObject player1;
private Vector3 oldVelocity;
public Rigidbody rb;
public GameObject bullet;
void Start () {
GameObject Player = GameObject.Find("Player 1");
Shooting playerScript = Player.GetComponent<Shooting>();
rb = GetComponent<Rigidbody>();
rb.velocity = playerScript.m_FireTransform.forward * 15;
rb.freezeRotation = true;
}
void Update () {
GameObject Player = GameObject.Find("Player 1");
Shooting playerScript = Player.GetComponent<Shooting>();
if (range < 0) {
Destroy (gameObject);
playerScript.ammo += 1;
}
range --;
oldVelocity = rb.velocity;
}
void OnCollisionEnter(Collision collision){
ContactPoint contact = collision.contacts [0];
Vector3 reflectedVelocity = Vector3.Reflect (oldVelocity, contact.normal);
rb.velocity = reflectedVelocity;
Quaternion rotation = Quaternion.FromToRotation (oldVelocity, reflectedVelocity);
transform.rotation = rotation * transform.rotation;
}
}
Answer by Raresh · Nov 13, 2016 at 08:53 AM
1) Make a new layer called "Bullets"
2) Go to Edit -> Project Settings -> Physics and under the Layer Collision Matrix, untick the box that has the row and column for Bullets (Should be on the anti-diagonal)
Now your bullets will pass through each others
In case you're doing this in 2D remember to select Physics2D and not simply Physics.