Get rotation from hit object

Hey there

I am trying to achieve some things with a projectile:

  1. Destroy on collision -check
  2. Ragdolloze hit object -check
  3. 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

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