Ok, so I have been playing with creating guns in unity and I want to create bullet penetration, I have seen a few ideas on how to implement this but they are all for raycast guns, I’m spawning a bullet object and launching it. The only way I can think to do this would be to make the collider for the bullet a trigger upon colliding an object with the tag “Penetrable”, However this rarly actually works and it causes the whole game to lag, any ideas?
Script:
using UnityEngine;
using System.Collections;
public class BulletObject : MonoBehaviour {
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Penetrable")
{
collider.isTrigger = true;
print (this.gameObject.name +" Has had it's spherecollider removed");
}
if (collision.gameObject.tag == "Terrain")
{
print (this.gameObject.name + " has hit the ground!");
Destroy (this.gameObject);
}
}
void OnCollisionExit(Collision collision)
{
if (collision.gameObject.tag == "Penetrable")
{
collider.isTrigger = false;
print (this.gameObject.name + " Has had it's spherecollider re-added");
}
}
}
If you want to pass through things, let the bullet be a trigger all the time and use OnTrigger events instead. You can add a regular rigidbody to it and apply forces or set rigidbody.velocity in order to move the bullet. If you want it to bounce off of non-penetrable surfaces, however, things may become complicated: you should calculate yourself the hit point and reflect the velocity about the normal. Another problem you may have is the “tunnel effect”, when a fast rigidbody simply passes through thin colliders without generating any collision or trigger event (the rigidbody is before the collider in one physics cycle and after it in the next).
Fortunately, there’s a solution that solves both problems: let the bullet be a simple mesh (no collider), calculate the trajectory each frame and check collisions with Linecast. The bullet script could be like this:
using UnityEngine;
using System.Collections;
public class BulletScript: MonoBehaviour (
public float bulletSpeed = 100;
public float gravity = 9.8f;
public float range = 1000;
IEnumerator Start(){
Vector3 vel = bulletSpeed * transform.forward; // calculate velocity vector
Vector3 pos = transform.position;
float dist = 0; // initialize distance travelled
while (dist < range){
yield return null; // let Unity free till next frame
vel.y -= gravity * Time.deltaTime; // apply gravity
Vector3 newPos = pos + vel * Time.deltaTime; // calculate current position
float thisDist = Vector3.Distance(pos, newPos); // get the distance since last frame
dist += thisDist; // update total distance
// check collision
RaycastHit hit;
if (Physics.Linecast(pos, newPos, out hit)){ // if something hit...
switch (hit.transform.tag){
case "Penetrable":
// object is penetrable
print("Penetrating " + hit.transform.name);
break;
case "Terrain":
// hit the ground: delete bullet (coroutine dies too)
Destroy(gameObject);
break;
default:
// other objects - bounce off:
vel = Vector3.Reflect(vel, hit.normal); // reflect velocity
thisDist -= hit.distance; // calculate distance from surface
// find new position after bouncing:
newPos = hit.point + thisDist * vel.normalized;
break;
}
}
transform.position = pos = newPos; // update position
transform.forward = vel; // align bullet to the movement direction
}
// shot ended because it's out of range
Destroy(gameObject); // bullet suicides
}
}
In order to create the bullet prefab, get the bullet object you already have and remove collider, rigidbody and everything else but the MeshFilter and the MeshRenderer. Attach the script above to the bullet and drag it to the Project view to make it a prefab. When you want to fire the bullet, just instantiate it at the desired position and pointed to the target - the bullet moves by itself.