- Home /
Rotating a character relative to the ground's rotation
Hello I'm new to unity and I'm attempting to create my first game. I've been trying to figure this out for two days now but haven't made any progress. In the game the character runs up a hill that gets steeper and steeper; I'm trying to smoothly rotate the character relative to the ground's x rotation. Here's my script so far.
void Start(){
//bool rotate = false;
float rotSmoothing = 0.5f;
}
// Update is called once per frame
void Update () {
RaycastHit hit;
if(Physics.Raycast(transform.position, -Vector3.up, out hit, 3)){
Quaternion Rot = Quaternion.Slerp (transform.rotation,
hit.collider.gameObject.transform.rotation,rotSmoothing * Time.deltaTime);
transform.rotation = Quaternion.Euler (-Rot.eulerAngles.x,transform.eulerAngles.y,transform.eulerAngles.z);
Debug.DrawRay (transform.position, -Vector3.up * 3, Color.red);
}
}
Answer by Nighfox · Jan 27, 2018 at 05:19 AM
Simply remove the negative sign in front of Rot.eulerAngles.x
:
transform.rotation = Quaternion.Euler(Rot.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z);
Another thing I noticed, in the second parameter of Quaternion.Slerp
, you can just do: hit.collider.transform.rotation
instead of hit.collider.gameObject.transform.rotation
.
Thank you for responding but the negative sign was purposely added because of the orientation of the hill relative to the character. I might be new to unity but do you really think I'd be stuck for two days if I simply needed to remove a negative sign. Depending on how you read that, it may come off as passive aggressive but I'm saying it in the nicest way possible. I really do appreciate your response.
That still completely makes no sense. You're basically flipping the target orientation of the ground incorrectly, which will make the character just stop, or worse, even jitter. Without the sign, it just re-orients fine no matter what orientation the player or the ground has, regardless of relativity.
PS: I've tested it, and it works fine without the sign. And btw, no need to take this offensively. FYI even the pros sometimes get stuck on simple things.