RaycastHit returns True on nothing.

hi guys,

i got a bullet impact script using raycastHit.point to find the place where to put my decals or sparks.
the problem is, when i press “fire” i dont even have the time to see the bullet it sparks immediately.
why ?

the script is there:

var bulletExplosion : Transform;
var point : Vector3;
var explosionRotation : Quaternion;


function Update() {
		
		var hit: RaycastHit;
		var fwd = transform.TransformDirection(Vector3.forward);
		
		if (Physics.Raycast(this.transform.position, fwd, hit,1)) {
			
			point = hit.point;
			explosionRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
			Explode();
		}

	}
	
function Explode(){
	
	Destroy(this.gameObject);
	var instanBulletExplosion = Instantiate(bulletExplosion, point, explosionRotation);

	}

strangely when i use a “collision” script it works well, but the precision is terrible, so terrible that it dont even show the sparks 8 time on 10.
here is the “collision” code:

var explosion : Transform;


function OnCollisionEnter(collision : Collision){

	if (collision.gameObject.tag == "decor") {
	var contact = collision.contacts[0];
	var rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
	var pos = contact.point;
	Instantiate(explosion, pos, rot);
	Destroy(gameObject);
	
	}

}

can someone help me out ? to make the the raycast work properly or to make the collision script be more accurate.

thank you,

PARADOKS

Too newbie or too hard question ?

I am deadly stuck…

help me please.

Most likely, the raycast hits the model of the thing that fires the bullet. You should make sure that your raycast ignores the GameObject that fired it.

I cant figure out what is the raycast hitting.

i got this hierarchy :

-First Person Controller
-------Main Camera
----------Weapons
--------------MyWeapon

MainCamera, Weapons and MyWeapons are empty gameObjects with no collider on them.

the myWeapon gameObject has a script wich instantiate the bullet.

and finally the raycast script in on the bullet.

btw, when i use a normal collider it works, the bullet dont collide before hitting a wall.

thx

The RaycastHit object you get from the raycast contains lots of information on the hit:

For example, in your case, hit.gameObject will give you the game object that was hit by the ray.

as you said before, the Raycast was touching the object. I put the bullet object in the “ignore raycast” layer and it work well.

thank you Tomvds.

BTW, can someone explain me how the raycast can hit an object for where the ray is casting ?

Not sure what you mean by that - can you explain a bit further?