Raycast bullet impact and instantiating particles

Hi,

I’ve the following code that works, but in a extrange way!
I’ve tried the same but using OnCollision enter instead the raycast.and works a bit better, but on both, come impacts appear on the oposite side of the object that the bullet colide with.

In raycast the bullet decals works fine. but the particle emiters appear away of the impact point.

Can somebody check this code and tell me what is wrong?

Thanks and sorry for my english!

var imp_tierra: GameObject;
var imp_piedra: GameObject;
var imp_metal: GameObject;
var imp_sangre: GameObject;
var impacto : GameObject;
var dir_frz : Vector3;

function Update(){
	comprueba_colision();
}

function Start(){
	Destroy(gameObject, 15);
}
function comprueba_colision(){
	var direction = transform.TransformDirection(Vector3.forward);
	var hit : RaycastHit;
	dir_frz=direction;
	// Did we hit anything?
	if (Physics.Raycast (transform.position, direction, hit, 2)){
		Debug.Log("Impacto");
		impacta(hit);
	}
	
}
function impacta(col : RaycastHit) {
	//Destroy(gameObject);
	
	var instParticulas : GameObject;
	var instImpacto : GameObject;
	if (col.collider.tag=="Enemigo"){
		instParticulas=Instantiate(imp_sangre,col.point,Quaternion.FromToRotation(Vector3.up ,col.normal));
		instParticulas.transform.parent=col.transform;
		instImpacto=Instantiate(impacto,col.point,Quaternion.FromToRotation(Vector3.up ,col.normal));
		instImpacto.transform.parent=col.transform;
		instImpacto.transform.Translate(Vector3.up * 0.01);
		if (col.rigidbody)
		col.rigidbody.AddForceAtPosition(50 * dir_frz, col.point);
		Destroy(gameObject);
	}
	if (col.collider.tag=="Terreno"){
		instParticulas=Instantiate(imp_tierra,col.point,Quaternion.FromToRotation(Vector3.up ,col.normal));
		instImpacto=Instantiate(impacto,col.point,Quaternion.FromToRotation(Vector3.up ,col.normal));
		instImpacto.transform.Translate(Vector3.up * 0.01);
		Destroy(gameObject);
	}
	if (col.collider.tag=="Piedra"){
		instParticulas=Instantiate(imp_piedra,col.point,Quaternion.FromToRotation(Vector3.up ,col.normal));
		instImpacto=Instantiate(impacto,col.point,Quaternion.FromToRotation(Vector3.up ,col.normal));
		instImpacto.transform.Translate(Vector3.up * 0.01);
	}
	if (col.collider.tag=="Metal"){
		instParticulas=Instantiate(imp_metal,col.point,Quaternion.FromToRotation(Vector3.up ,col.normal));
		instImpacto=Instantiate(impacto,col.point,Quaternion.FromToRotation(Vector3.up ,col.normal));
		instImpacto.transform.Translate(Vector3.up * 0.01);
	}
}

I couldn’t find anything wrong in your code. In the “enemigo” case, you may have problems with the enemy collider - if it’s a capsule collider, the impact point may appear very out of position, since col.point will show the collider’s hit point, not the mesh’s. The same applies to any object with simple colliders (box, sphere etc.); the terrain and objects with mesh colliders should be ok. The particle direction problem may be caused by objects which were already rotated to the “correct” position when they became prefabs: Instantiate will replace any rotation they previously had by the one you’ve defined (from Up to Normal). I tested this: I created a bloody particle system with Local Velocity 10 in the Y direction only, and instantiated it at the raycast hit point with the rotation Vector3.up to col.normal, exactly like yours, and the blood splashed in the surface normal direction, as expected. I rotated the prefab 90° around the X axis, but the blood still splashed in the same direction. If your blood prefab originally was emitting in the X direction, for example, you can use Vector3.right instead of Vector3.up.

Thanks a lot, aldonaletto!!!

i already found that the bullet object was rotated and the raycast poin to the wrong direction.

So this issue is solved. I’ve modified the rotation and works perfectly!
But we still have the problem of the colliders that as you said, will be out of the mesh.

So how could we control the mesh collision?

Thanks man!

Ok! I’ll try some code to solve this, because i’ve character models with ragdoll and i still looking for i way to use the ragdoll colliders for this issue.
But the problem is that these collidrers Affect to the mesh movement in the animation. So i’ll have to find another vision to find the way.

For the bullet decals in the character object. If we’ve to use the coliders it will be more hard that if we find the way to transform the vector 3 coordinates of the impact point to the uv coordinates of the textere and paint a bullet hole directly. But i have a lot of reading and searching work ahead.

Thanks again, and if you have some advice for all this stuff, please tell me.

Cheers!