Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // find current surface angle
- RaycastHit hit;
- Ray ray = new Ray(transform.position,Vector3.down);
- LayerMask layerMask = ~(1 << gameObject.layer);
- if (Physics.Raycast(ray, out hit, 100, layerMask))
- {
- //Basically, get the angle between transform.up and Vector3.up.
- float angleDiff = Vector3.Angle(transform.up, hit.normal);
- // get its cross product,
- //(which is the axis of rotation to get from one vector to the other.)
- Vector3 cross = Vector3.Cross(transform.up, hit.normal);
- //Finally, apply torque along that axis according to the magnitude of the angle.
- rigidbody.AddTorque(cross * angleDiff * 10000);
- }
Advertisement
Add Comment
Please, Sign In to add comment