- Home /
How can I fix a terrain hugging character so that it maintains it's original orientation, but allows rotation?
I am aware that this question comes up several times, and in fact there is a go-to Answer that is referenced for everyone to look at. However, I've got it implemented, but I have a couple of issues with this code.
This is the code base I'm using... and it works. A little too well, really. I'm trying to modify it to match my character's original rotation, while still keeping the "up" direction modification. I tried an adjustment Vector3, but that just rotated my character in odder ways.
My own version of the code is modified only slightly, so that the rotation happens just on the ground, and other factors handle in-air rotation. The original orientation - that I want to keep, is Vector3(0, 90, 0).
void quadRaycastMotor() {
if (Player_Controller.characterController.isGrounded) {
Physics.Raycast(backLeft.position + Vector3.up, Vector3.down, out lr, 10f, LayerMask.NameToLayer("Ground"));
Physics.Raycast(backRight.position + Vector3.up, Vector3.down, out rr, 10f, LayerMask.NameToLayer("Ground"));
Physics.Raycast(frontLeft.position + Vector3.up, Vector3.down, out lf, 10f, LayerMask.NameToLayer("Ground"));
Physics.Raycast(frontRight.position + Vector3.up, Vector3.down, out rf, 10f, LayerMask.NameToLayer("Ground"));
upDir = (Vector3.Cross(rr.point - Vector3.up, lr.point - Vector3.up) +
Vector3.Cross(lr.point - Vector3.up, lf.point - Vector3.up) +
Vector3.Cross(lf.point - Vector3.up, rf.point - Vector3.up) +
Vector3.Cross(rf.point - Vector3.up, rr.point - Vector3.up)
).normalized;
}
upDir = new Vector3 (upDir.x, 0, 0);
Debug.Log (upDir);
transform.up = upDir;
}
Can anyone suggest a way to maintain the character rotation while applying ground rotation?
Answer by whydoidoit · Feb 19, 2014 at 07:26 AM
You could try just modifying it using FromToRotation:
//upDir = new Vector3(upDir.x,0,0);
transform.rotation *= Quaternion.FromToRotation(transform.up, upDir);
I fixed it after getting creative. Added conditional checks to each cast with it's own true/false variable, checking the tag of the objects being hit. Added a final "All are right" bool, and then only perform the calculations when everything is correct. I also took the character mesh and put it into a dedicated gameobject.
Your answer
Follow this Question
Related Questions
Generate mesh from raycast positions, independent of rotations 0 Answers
How to place a GameObject with mesh on a mesh "perfectly" through c# script? 1 Answer
Mesh Collider Issue(?) - Raycast (ScreenPointToRay) Appears to Collide on Nothing 0 Answers
Vertical auto-aim with raycasts 1 Answer
Rotation+BoxCast works with Update in game editor, but not when built or in FixedUpdate. 1 Answer