rotate object to collider's normal on collision

Not sure if ‘normal’ is the correct term or not, but imagine a fly running into a spiderweb. I want to rotate the fly so that it matches the collider’s rotation.

I also need to be able to do the same thing when colliding with a mesh…rotate to the surface of the part of the mesh the fly collided with.

Any help would be appreciated…I’m hitting a brick wall.

‘Normal’ is indeed the correct term. You want Collision.contacts…in particular note the second code example there.

–Eric

sorry, a little unclear on what you’re asking-

Do you mean you want to transition the fly from going head-on into the web to having its side squashed into the web? If so, realize that there are actually 360 degrees of squashed orientation you have to choose from (imagine balancing a pencil on its tip and letting go- it can drop in almost any direction)

To instantly rotate it so that it looks like it’s landing on the web:

function OnCollisionEnter(collision : Collision) 
{
  var contact = collision.contacts[0];
  var rot = Quaternion.FromToRotation(fly.transform.up, contact.normal);
  fly.transform.rotation *= rot;

}

To make the fly upside down, change fly.transform.up to -fly.transform.up. To make it on one of its sides, change it to fly.transform.right.

If you want to do this over time, I’d toggle a boolean to indicate my intention, then do it incrementally over a number of frames in Update.

HTH!

heh, I took so long responding, Eric beat me to it :smile:

Kudos to you Eric!

Thanks to both of you! This is exactly what I needed!! :slight_smile: