- Home /
Get rotation from hit object
Hey there
I am trying to achieve some things with a projectile:
Destroy on collision -check
Ragdolloze hit object -check
leave the impact effect on object
a. instantiate the impact effect -check
b. at the hitpoint -check
c. align impact effect with the surface - X
It is frustrating ,becouse the position is perfect and calculated from the hit.contacts contact.point,i just cant get the rotation to work.
Here is my code:
var impactEffect : GameObject;
function Start () { //auto destroy to prevent infinite projectiles
yield WaitForSeconds(2.0);
Destroy (gameObject);
}
function OnCollisionEnter ( hit : Collision){
Destroy (gameObject);
Debug.Log(hit.gameObject);
for (var contact : ContactPoint in hit.contacts) {
var ieffect = Instantiate (impactEffect, contact.point,transform.rotation); //rotation is a problem
ieffect.transform.parent = hit.gameObject.transform;
}
hit.gameObject.rigidbody.isKinematic = false; //ragdollize hit object
hit.gameObject.SendMessageUpwards("Ragdollize");
}
I tried using this :
var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
But i just dont know how to get this to work.
Any help would be greatly appriciated.
Thanks
Answer by Bunny83 · Mar 31, 2012 at 10:44 AM
The easiest way is to setup your hit-effect prefab looks along the forward axis (the blue z axis). That way you can use LookRotation with hit.normal
Something like that:
Instantiate (impactEffect, contact.point, Quaternion.LookRotation(contact.normal));
Keep in mind that in this case the z-axis will be aligned to your hit normal
Thanks for the quick reply! Could you please show me how would you exactly implement this into the above script? Im new to this stuff, and changing it to your solution wont spawn it at all.
I suspect i have to give a variable or something, but i dont know how.
You could use only hit.contacts[0] - replace the whole for loop with this code:
...
var contact : ContactPoint = hit.contacts[0];
var ieffect = Instantiate (impactEffect, contact.point, Quaternion.LookRotation(contact.normal));
ieffect.transform.parent = hit.gameObject.transform;
...
But remember what @Bunny83 said: your particle effect must emit in the Z direction, because LookRotation will align this direction to the surface normal.
Thanks again ,it worked ,but i think i see now what you meant. The effect is always 90 degrees wrong, but i dont know how to set the axes, could you please help me out on that one aswell? Just rotating it in the prefab doesnt work.
Jep, sorry about that. I've just confused Collision and RaycastHit ;)
Sure you can rotate it in the prefab. $$anonymous$$aybe you need to parent the actual effect to an empty gameobject and turn this into a prefab.
It's important that the effect points in the direction of the blue (forward) axis.
Yes ,it works perfectly with a particle effect, but im trying to instantiate a mesh (WaterPlane_$$anonymous$$esh as a test),and it always spawns 90° wrong on the X axis. Changing in the prefab wont help :\ I only want this as a tiny effect, like a bullethole which lingers. I couldnt do anything with projectors ,they acter weird, projecting bloody everywhere, so i tried this.
I appriciate all your help. :)