- Home /
Maintain local transform.rotation.y when updating transform.up?
I am trying to rotate my character to match the ground normal, which is working, but it resets the character's local rotation.y, and I understand that updating the transform.up of the character resets it, but how do I maintain the character's y rotation? I'm using the following code to match the ground and rotate:
//Ground Match
RaycastHit hit;
if (Physics.Raycast (transform.position, -transform.up * 5, out hit)) {
if (transform.up != hit.normal) {
transform.up = hit.normal;
}
if (hit.distance < 1.25) {
transform.Translate (0, Time.deltaTime, 0);
}
}
/*Handle Core Rotation*/
if (h > 0 || h < 0) {
m = 0;
r += (h * handling) / 90;
} else if (h == 0) {
r = Mathf.Lerp(r, 0.5f, m);
m += 0.1f;
}
/*Limit Rotation*/
if (r > 1) {
r = 1;
} else if (r < 0) {
r = 0;
}
rotateConstant = Mathf.Lerp (-1, 1, r);
if (currentSpeed == 0) {
rotatePower = rotateConstant * ((handling / 5) * 4);
} else {
rotatePower = (rotateConstant * handling) * 2;
}
//Final Rotate
transform.Rotate(0, rotatePower, 0)
Answer by UnityCoach · Nov 01, 2018 at 08:35 AM
The easiest way to do what you want is to use Quaternion.LookRotation().
Pass it your current forward direction and ground normal as up vector, it'll return a rotation.
Something like this :
transform.rotation = Quaternion.LookRotation(transform.forward, hit.normal);
So, this does allow me to match the ground and rotate my character, but it creates an issue where the character doesn't move along the transform.forward now, but ONLY after updating from the normal of the default starting ground. I'm using this statement to move forward constantly: transform.position += transform.forward * currentSpeed;
A couple of notes from what I see in your code :
RayCast second argument is a direction vector (expected to be normalised), multiplying it by 5 doesn't do anything. You probably want to pass a distance, that it another argument, after the RaycastHit.
Passing -transform.up for direction means if your character tilts, it'll possibly cast the ray in front of it or behind it. You may want to use Vector3.down ins$$anonymous$$d.
I see you use
transform.Translate ()
, do you also assigntransform.position
? You may have an issue there. Try usingtransform.Translate(0, 0, currentSpeed, Space.Self)
ins$$anonymous$$d.If you do this in two different scripts, $$anonymous$$d the execution order too.
Hope this helps.
Thank you for helping to clean things up, but this doesn't fix the player not moving in the proper direction when on a slope. I feel like it has something to do with these lines, but I am clueless as to what the actual problem is and how to fix it: Vector3 forward = Quaternion.Euler (transform.rotation.eulerAngles.x, rotatePower, transform.rotation.eulerAngles.z) * Vector3.forward;
if (!Input.GetButton ("Jump")) {
if (i < 1) {
i += (acceleration / 100) * 2;
}
transform.position += transform.forward * currentSpeed;
driftControl.transform.localRotation = gameObject.transform.localRotation;
}
/*Handle Drifting*/
else if (Input.GetButton ("Jump")) {
if (i > 0) {
i -= (brakePower / 100);
} else if (i <= 0) {
i = 0;
}
transform.position += ((driftControl.transform.forward + transform.forward) / 2) * currentSpeed;
}
transform.LookAt (transform.position + forward);
transform.rotation = Quaternion.LookRotation(transform.forward * currentSpeed, hit.normal);
Answer by SunnyChow · Nov 01, 2018 at 09:07 AM
Do you mean transform.localEulerAngles/transform.eulerAngles ? transform.rotation is Quaternion not angles. If you really want to have full control on y angle when dealling with ground normal, maybe you can use Quaternion.AngleAxis()
Your answer
Follow this Question
Related Questions
Error CS1525 Unexpected symbol `float' 1 Answer
C# script parse error 1 Answer