- Home /
Tilt character with the ground and rotate it
Hi.
I am making a game where the character tilts with the ground. I managed to do it pretty smoothly with the code below which uses Raycasts from 4 sub-objects. For some reasons, it is best I keep a Character Controller.
public Transform backLeft;
public Transform backRight;
public Transform frontLeft;
public Transform frontRight;
private RaycastHit lr;
private RaycastHit rr;
private RaycastHit lf;
private RaycastHit rf;
private Vector3 upDir;
void Update () {
Physics.Raycast(backLeft.position + Vector3.up, Vector3.down, out lr);
Physics.Raycast(backRight.position + Vector3.up, Vector3.down, out rr);
Physics.Raycast(frontLeft.position + Vector3.up, Vector3.down, out lf);
Physics.Raycast(frontRight.position + Vector3.up, Vector3.down, out rf);
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;
transform.up = upDir;
}
But the problem now is that the character can't rotate anymore (I am using transform.Rotate with left/right arrow keys). I understand this is because I reassign transform.up, but I haven't found a solution for now. I've tried several things, but none worked. The most satisfying was using a temporary variable to keep the rotation on y axis, but then the character isn't aligned with the correct normal anymore after he changes his starting rotation.
Has someone got an idea?
Answer by whydoidoit · Oct 13, 2013 at 04:13 PM
Rotate the position of the up vector like this:
transform.rotation = Quaternion.FromToRotation(transform.up, upDir) * transform.rotation;
I believe the order matters in this one:
transform.rotation = Quaternion.FromToRotation(transform.up, upDir) * transform.rotation;
I've always assumed (based on a few tests) that assigning to transform.up was doing a FromTo under the hood. Not the case?
Really - that's not what I do - so given that the transform.up is already transformed, surely you should apply the rotation of it to that?
You have a much deeper understanding of Unity than I do, so I keep thinking I'm missing something. But I cannot see this order working. Apply the following script to any object where you can identify the top of the object. It generates a random rotation then attempts to align the resulting object with Vector3.up. Left mouse button has FromToRotation() first, right mouse button has it second.
#pragma strict
function Update () {
if (Input.Get$$anonymous$$ouseButtonDown(0)) {
Debug.Log("FromTo exectued first");
transform.rotation = Random.rotation;
transform.rotation = Quaternion.FromToRotation(transform.up, Vector3.up) * transform.rotation;
}
else if (Input.Get$$anonymous$$ouseButtonDown(1)) {
Debug.Log("FromTo exectued second");
transform.rotation = Random.rotation;
transform.rotation = transform.rotation * Quaternion.FromToRotation(transform.up, Vector3.up);
}
}
I must be double negging my self (do it the opposite way to the way you feel it should work! Guess I've got used to it feeling the right way).
I just did this today and did it the other way around which mostly works, but of course you are right, everywhere else I have it is the correct way around. Ugh, that's what comes of not sleeping enough and being stuck at the keyboard.
Indeed, the solution of robertbu seems to be working perfectly. Thank you very much.
Your answer
Follow this Question
Related Questions
Camera lock on enemy 0 Answers
CharacterController "breaks" collision, goes haywire 2 Answers
Face Direction Movement using CharacterController 1 Answer
Rotate a vector3 to a surface normal? 1 Answer
rotate around character 1 Answer