- Home /
Question by
formidable · May 13, 2012 at 08:21 PM ·
directiongloballocalrotationslide
Help with changing global direction to local
When the character is grounded, it always faces one global direction. How do I make it so that I can modify his rotation.
//ALIGN WITH TERRAIN
var isGrounded: boolean = false;
var hit: RaycastHit;
var theRay: Vector3;
var smoothTime : float = 0.3;
function FixedUpdate ()
{
if(isGrounded)
{
// Convert from local space to world space
theRay = transform.TransformDirection(Vector3 (0,-1,0));
// Send ray from origin of transform. If the ray
// (facing downwards globally) hits something...
if (Physics.Raycast(transform.position, theRay, hit))
{
// Set the rotation so that the transform's y-axis
// is perpendicular to the normal
transform.rotation = Quaternion.FromToRotation(Vector3(0,1,0), hit.normal);
}
}
}
// character is touching the ground
function OnCollisionEnter(collision : Collision)
{
isGrounded = true;
}
// character gets airborn
function OnCollisionExit(collisionInfo : Collision)
{
isGrounded = false;
// Prevent character from spiraling out of control the second it goes airborn
rigidbody.angularVelocity = Vector3.zero;
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613071438im_/https://answers.unity.com/themes/thub/images/avi.jpg)