- Home /
Question by
Wurtstulle · Sep 01, 2016 at 05:13 PM ·
rotationrotatecharacter movementground
Character to Ground and Turn Rotation
Hey Unity Com!
What I want to do ? I want a simple 3rd Person Camera/Character Setup, but the Character rotates (beside Turn Rotation) depending on the Ground so he always "up" when he is f.e. walking Stairs
var rcHit : RaycastHit;
//Make raycast direction down
var theRay : Vector3 = transform.TransformDirection(Vector3.down);
if (Physics.Raycast(transform.position, theRay, rcHit)) {
//this is for getting distance from object to the ground
var GroundDis = rcHit.distance;
//with this you rotate object to adjust with terrain
transform.rotation = Quaternion.FromToRotation(Vector3.up, rcHit.normal) * transform.rotation;
}
Rotates Character based on Ground Angle to Character.
This works, also when I rotate using the Camera (version of Character Rotation I use)
if(Input.GetMouseButton(1)) {
transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
}
But what he is not doing is the normal Turn Rotation based on a Input
transform.Rotate (0, Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
This ends in a mess, propaply because of the Y Axis is now not the Up Vector based on my Character. Looked in the Forum but didnt found much. Would be awesome if someone can point me in the direction of a valid way :)
Comment
Answer by b1gry4n · Sep 01, 2016 at 11:43 PM
Untested code, let me know if it works
Vector3 rot = new Vector3(0, transform.eulerAngles.y + (Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime), 0);
transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal) * Quaternion.Euler(rot);