- Home /
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);
}
}
Answer by aldonaletto · Jul 02, 2011 at 06:40 PM
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.
If you have solid modeled objects like trucks, tanks, buildings etc., you can use meshs colliders - they follow the object format, and collisions are way more precise (but more expensive to the performance). Live creatures like enemies, monsters etc. should use a capsule or sphere collider, because they move parts like arms and legs, and the mesh collider could not be recalculated at every movement. You can child objects to the enemy to combine their colliders in a closer approximation to its body and head, but there's no way to follow the enemy's exact shape.
Answer by danielreyvega · Jul 02, 2011 at 07:23 PM
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!
I'm sorry i don't read the comments. I reply from my fphone and i canot see very well all the page!
(9 seconds ago)danielreyvega
Answer by danielreyvega · Jul 03, 2011 at 10:21 AM
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!
I'm sorry i don't read the comments. I reply from my fphone and i canot see very well all the page!
Your answer
Follow this Question
Related Questions
problem with instatiated clone 2 Answers
How To: Spawn object on raycast collision. 2 Answers
help with scripting combo points 1 Answer
checking collision with polygon collider using ray cast and instantiating objects issue 1 Answer
How can I continue to instantiate an object after deleting 0 Answers