- Home /
Smooth rotation on one axis only
Hello sexy.
In my third person game I have a few lines of code that looks in the cameras forward direction when the player moves forward and different directions when moving backwards and sideways.
A small sample of code:
(It is in the function Update()):
if(Input.GetAxisRaw("Horizontal") > 0.1)
{
transform.eulerAngles.y = GameCam.eulerAngles.y + 90;
transform.Translate(0,0,Input.GetAxisRaw("Horizontal")/20);
}
Here the player moves right, and the player aligns himself with the camera(GameCam) + 90 degrees, but it happens in a instant and I would very much like it smoothed out/damped, but I have no general idea how to do so. I do know Quaternions would be of good use(I think), but I do not know how to implement it.
Any help received would be appreciated.
Answer by testure · Jun 06, 2011 at 06:20 PM
In addition to skail's answer, I'll point out that you need to convert your euler angles to a quaternion first:
to.rotation = Quaternion.Euler(new Vector3(angleX, angleY, angleZ)); Quaternion.Slerp(from.rotation, to.rotation, Time.time * speed);
this can obviously be combined into one line of code- but for clarity's sake i've broken it up into two parts.
Good point, It did not give any errors in the console, but the player dose not look in the camera direction whatsoever now. Weird -.-´
if that's the case, your angles are probably not correct. If you're trying to make the player look at the camera, why not just use the transform.LookAt() method?
playerObject.transform.LookAt(Camera.mainCamera.transform);
If you needed it to interpolate you could even add some intermediate transform that gets interpolated, and look at that ins$$anonymous$$d.
Try printing out both the rotations: print(from.rotation); print(to.rotation); $$anonymous$$aybe also keep track of its rotation while its trying to slerp its way around, I have had some issues with slerping and just printing out those thigs usually helps me figure out what is wrong.
Answer by skail · Jun 06, 2011 at 04:23 PM
You could try using Quaternion.Slerp.
transform.rotation = Quaternion.Slerp (from.rotation, to.rotation, Time.time * speed);
So, true and thanks, but as said I only want on the Y axis and if you write it like(specificly tell it to rotate on the Y axis):
var turnSpeed = 0.1;
transform.rotation.y = Quaternion.Slerp (transform.rotation.y, GameCam.rotation.y, Time.time * turnSpeed);
It gives errors:
The best overload for the method `UNityEngine.Quaernion.Slerp(UnityEngine.Quaternion, UnityEngine.Quaternion, float)´ is not compatible with the argument list ´(float, float, float)´.
and:
Cannot convert ´UnityEngine.Quaternion to ´float´.
Those errors are occurring because a normal transform.rotation value has 4 values and when you reference only 1 of them it causes problems. $$anonymous$$ost likely what you will need to do is make another transform.rotation value to reference and only modify its.y value.
Your answer
Follow this Question
Related Questions
How can I instantiate a gameobject facing another gameobject 2D? 0 Answers
local rotation per axis between frames? 1 Answer
How to use quaternions to apply an offset to a rotation to calibrate a controller 1 Answer
Quaternions; how do I rotate an object around only 2 axis based on another rotation? 1 Answer
Get real compass direction 1 Answer