A Bullet's rigidbody, hit.normal

Q1.I have a problem with using a GameObject as a bullet instead of a Ray. I’d use Rays if it werent for it to be invisible and no flying bullets that are present.

The problem now with a rigidbody is that I want a bullethole to appear(a plane with a picture) where the bullet hits a part with the tag “LevelParts” and some sparks at the same point.

Now with a rigidbody the first thing is that you don’t have any hit.normal as in Rays. And then the collision detection doesnt record a collision if the bullet goes too fast.

Code attached to bullet:

 var maxDist : float = 1000000000;

 var decalHitWall : GameObject;

 var floatInfrontOfWall : float = 0.0001;

function Update () {
Destroy(gameObject, 3);

function OnTriggerEnter(hit : Collider){


hit.gameObject.SendMessage("OnDamage", null);


Debug.Log("Hit an enemy");


if(hit.tag == "LevelParts"){


Debug.Log("Hit a levelpart");



To get the bullet to record a collision every time I got to set the bulletspeed to about 1500 which is very slow, About 3000 is the normal speed I have on the bullet right now.

Help would be appreciated

Q2. Is there any difference between hit.tag or hit.gameObject.CompareTag(“”) ??

Q3. I have tried many ways of turning my bullet the way I want it to but it just points straight up in the air even tho it flies as it should to the right direction,

	var projectile = Instantiate(Bullet, transform.position, Quaternion.identity);
	projectile.rigidbody.AddForce(transform.forward * bulletSpeed);

The bullet’s tip is ^ but it should be pointing > or < depending on to what direction you shoot(2D platformer)

1- OnTrigger events don’t record collision info - they just pass the other collider reference. You should use OnCollisionEnter(col: Collision) to get more detailed information - usually we read col.contacts[0] and extract info from it, like below. To avoid the fast bullet problem, a simple trick is to make its collider very long (one or two meters) and ahead of the bullet, like this:

alt text

var bulletHolePrefab: Transform; // drag the bullet hole prefab here
var dust: ParticleEmitter; // drag a dust particle prefab here
var blood: ParticleEmitter; // drag the blood particle prefab here

function OnCollisionEnter(col: Collision){
  var part: ParticleEmitter;
  var contact: ContactPoint = col.contacts[0]; // get first contact point
  // calculate rotation from prefab normal to contact.normal:
  var rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
  if (col.gameObject.CompareTag("Enemy")){
    col.gameObject.SendMessage("OnDamage", null);
    Debug.Log("Hit an enemy");
    part = blood; // select blood particles
  if (col.gameObject.CompareTag("LevelParts")){
    Debug.Log("Hit a levelpart");
    part = dust; // select dust particles
    var hole = Instantiate(bulletHolePrefab, contact.point, rot);
    hole.parent = col.transform; // child the hole to the hit object
  if (part){ // if some particle effect selected, create it...
    var p: ParticleEmitter = Instantiate(part, contact.point, rot);
    p.Emit(); // and emit a chunk of particles
  Destroy(gameObject); // destroy bullet on any collision

NOTE: Since enemies usually have capsule colliders, attaching a bullet hole to them produces very weird effects - thus it’s better to do it only for simple geometry.

2- gameObject.CompareTag is faster than comparing tags directly, and doesn’t allocate memory - the tag names are actually stored in a Unity’s table, and must be converted to a string when we read the tag property.

3- You’re instantiating the bullet without any rotation (Quaternion.identity). Use transform.rotation instead:

  var projectile = Instantiate(Bullet, transform.position, transform.rotation);

Yea now its working better but not perfectly :confused: If i shoot on the normal wall with normal collider nothing happens, and on the wall which have a thicker collider it works sometimes but not always and almost half of the bullets appear at the end of the collider instead of in front of.

Thanks for your time btw :slight_smile:

I don’t know why but the bullethole is like sometimes appearing at the other side of the wall or in the middle, I think it would be fixed if I only slowed down the bulletspeed but that comes with a problem.

Even if I set the angular drag very low the bullets drop to the floor after just a meter or two. I don’t know how I’ma make it so that it would drop as normal after a long time in the air instead of dropping in front of the gun but still be slow enough to see it go and make the collision more accurate.

I think i can help you with the bulletHole code…

// You have to instantiate the bullet using another script
// Attach this script to bullet
// Edit it if you need
// You can raycast even when you are using physical bullet
// This will cast a ray which will be 2 units ahead of the bullet

var bulletHole: GameObject;
var rayDistance: float = 2.0f;  // Note that the ray will be 2 units ahead of the bullet
var maxHits: int = 4;

private var oldPos;
private var newPos: Vector3;
private var hasHit: boolean = false;
private var hitCount: int = 0;

function Start () 
	oldPos = transform.position;
	newPos = transform.position;

function Update () 
	if (hasHit) {
	newPos = transform.position;
	var hits : RaycastHit[] = Physics.RaycastAll(newPos, transform.forward, rayDistance);
	for (var i: int = 0; i < hits.Length; i++)
		var hit: RaycastHit = hits*;*
  •  newPos = hit.point;*
  •  hitCount++;*
  •  var holeRotation: Quaternion = Quaternion.FromToRotation(Vector3.up, hit.normal);*
  •  var holeInstance = Instantiate(bulletHole, hit.point, holeRotation);*
  •  if (hitCount >= maxHits) {*
  •  	hasHit = true;*
  •  	Destroy(gameObject);*
  •  	return;*
  •  }*
  • }*