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);
}
}