The question is answered, right answer was accepted
How to make a smooth rotation?
Hello, I have a problem with the code. The problem is when I press either on left/right of the screen the character doesn't rotate smoothly, but I also have a code where you press "A" or "D" and the character rotates smoothly. Can someone help me understand what I am doing wrong please?? My code:
transform.Rotate(0, 0, Input.GetAxis("Horizontal") * rotateSpeed);
if (Input.GetMouseButton(0))
{
if (Input.mousePosition.x > Screen.width / 2)
transform.Rotate(0, 0, -rotateSpeed);
else
transform.Rotate(0, 0, rotateSpeed);
}
transform.rotation = Quaternion.Lerp(transform.rotation, transform.rotation, rotateSpeed * Time.deltaTime);
Thank you for you time
Answer by SarperS · Aug 07, 2016 at 10:57 PM
You are trying to lerp to the same position you are already in. Try this and let me know if there is anything you need explained.
private float desiredRot;
public float rotSpeed = 250;
public float damping = 10;
private void OnEnable() {
desiredRot = transform.eulerAngles.z;
}
private void Update() {
if (Input.GetMouseButton(0)) {
if (Input.mousePosition.x > Screen.width / 2) desiredRot -= rotSpeed * Time.deltaTime;
else desiredRot += rotSpeed * Time.deltaTime;
}
var desiredRotQ = Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, desiredRot);
transform.rotation = Quaternion.Lerp(transform.rotation, desiredRotQ, Time.deltaTime * damping);
}
It does work, but not exactly how I intended for it to. I am making the object rotate on it's Z axis. For example, I have my object going forward but rather than making it turn left/right I want it to turn the z axis (up/down). In a humanoid perspective (if we turn it 180 degrees) it's head will be where his feet were and his feet will be where his head was.
It shouldn't be hard to change the two lines in the given code, did you actually read and try to understand it? Anyways I have edited the original answer please try it now.
Yeah, that is my fix too. I literally just got that. Thanks a bunch.
Sorry, never $$anonymous$$d. I was hoping to get spoon fed but I realised it was a very bad thing. Then I tried to fix it myself, it is perfect now works fine. Thank you very much with your help! Helped me understand a bit more how to use the Lerping :)
Hey no worries, I've already lashed out before you commented so.. :) Please mark it as answered.
Sadly the forum questions/comments don't refresh so you don't see it update.
-Thought the up vote marked it as answered - then I saw the accept. Accepted :)
That is not how you use Lerp. Time.deltaTime * damping (t value) is wrong in so many ways. First of all if you don't smoothly increment the t value you will simply not use Lerp. Second of all since Time.deltaTime is not fixed value it will go up and down, the movement will be vibrating.
You're "wrong in so many ways".
About incrementing: Value is smoothly incremented in this example because transform.rotation is moving each frame toward the target. val = lerp(val, target, deltaTime) is a valid way to use lerp. It's not actually a "linear" interpolation, as it is moving slower and slower when getting closer to the target, never reaching it. But it's a fast and nice solution for many tasks.
About delta time: Time.deltatime is used to S$$anonymous$$OOTH the vibrating you are talking about. This is the very basics of constant movement - you multiply any movement by this value to compensate a variable framerate.
Frame A took 1ms, and frame B took 4ms. Without using delta time, you will rotate a fixed value each frame, but for a different time, so the velocity of this rotation each frame is different too.
I think you should try to understand Lerp, instead of writing wrong stuff. Say:
float x = $$anonymous$$athf.Lerp(0f, 1f, 0.25f)
//this will ***ALWAYS*** give the result x= 0.25f;
Just try and see, if you don't believe me. And in the case above:
float x = $$anonymous$$athf.Lerp(0f, 1f, Time.deltaTime)
// deltaTime will be approximately (for 30fps) 1/30 = 0.0333
// so the result will always be x = 0.3333f;
That is simple mate! x will not go to 0, x will not go to 1 and it will not smoothly do anything. You will just decieve yourself.
The correct use of time.deltaTime in movement is a totally different story. It is not about Lerp.