- Home /
How to smoothly rotate an object on only two axes?
This is a function that is supposed to only reset the rotation of the player gameobject on the x & z axes. I passed the value of the player's current y-axis rotation because I thought it would maintain that rotation, but instead the y-axis also ends up moving towards 0. I'm not sure if I'm doing something wrong with my Quaternion declaration or my usage of slerp.
void centerRotation(){
//rotate player character from current rotation in x & z axis To target rotation on x & z
Quaternion target = Quaternion.Euler(0,transform.rotation.y,0);
transform.rotation = Quaternion.Slerp(transform.rotation, target, rotSpeed*Time.deltaTime);
}
Answer by End_Line · Oct 11, 2020 at 06:08 PM
lol, I figured it out. I was using transform.rotation.y when I needed to be using transform.eulerAngles.y rotation.y just returns a value between 0 and 1, eulerAngles gives you the actual rotation in degrees.
Answer by xibanya · Oct 11, 2020 at 04:18 AM
Use Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target, Vector3.up), rotSpeed * Time.deltaTime);
Thanks I think this helped a bit, but I had to make a small change though. It didn't let me pass target into LookRotation since it was a quaternion so I tried using Transform.forward instead, but that just made the gameobject rotation 0 on the z axis and then it maintained the x & y axes of whatever the last rotation was. I'm gonna try out some vector math stuff, but if there's a way to do it with the quaternion that'd be great.
To look at a target position, use Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetTransform.position - transform.position, Vector3.up), rotSpeed * Time.deltaTime);