duck

duck

Jun 9th, 2010
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.78 KB | None | 0 0
  1.         // find current surface angle
  2.         RaycastHit hit;
  3.         Ray ray = new Ray(transform.position,Vector3.down);
  4.  
  5.         LayerMask layerMask = ~(1 << gameObject.layer);
  6.  
  7.         if (Physics.Raycast(ray, out hit, 100, layerMask))
  8.         {
  9.  
  10.             //Basically, get the angle between transform.up and Vector3.up.
  11.             float angleDiff = Vector3.Angle(transform.up, hit.normal);
  12.                            
  13.             // get its cross product,
  14.             //(which is the axis of rotation to get from one vector to the other.)
  15.             Vector3 cross = Vector3.Cross(transform.up, hit.normal);
  16.  
  17.             //Finally, apply torque along that axis according to the magnitude of the angle.
  18.             rigidbody.AddTorque(cross * angleDiff * 10000);
  19.  
  20.         }
Advertisement
Add Comment
Please, Sign In to add comment