- Home /
Smoothed Rotation with 2D Top-Down View
I was wondering how I could convert code that makes the character simply face the direction that the axis is facing:
if(direction.sqrMagnitude > 0.1f) { transform.LookAt(transform.position + direction); }
To code that rotates the character at a pre-determined velocity toward that rotation, so as to make the character rotation "smoothed".
Answer by robertbu · Aug 13, 2013 at 02:52 AM
At the top of the file:
var qTo : Quaternion;
var speed = 85.0; // Degrees per second
Wherever you are setting the direction:
if (direction.sqrMagnitude > 0.1f) {
qTo = Quaternion.LookRotation(direction);
}
Then in Update():
transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, speed * Time.deltaTime);
This worked perfectly. Thank you so much!!
$$anonymous$$ind if I ask another question? Good.
I want to accomplish the same thing, but with mouse input now. I want the player to be able to rotate towards looking at the mouse position as well..I have the mouse input and joystick input code separated, I understand how that works.
But how would I implement this system given a mouse position?
THAN$$anonymous$$ YOU SO $$anonymous$$UCH <3
I just answered this question about facing the mouse position:
http://answers.unity3d.com/questions/513787/rotate-an-object-to-face-mouse.html
This question uses Transform.LookAt(). The difference between LookAt() and LookRotation() is that LookAt() uses a position in 3D space where LookRotation() uses a direction vector. So to get your direction as used in the above code to use in LookRotation():
direction = position - transform.position;
...where 'position' is the variable in the script at the link.
Thanks man. I really appreciate it. I've learned so much about Unity through the Answers site.
Your answer
Follow this Question
Related Questions
Start Coroutine after other has finished 4 Answers
Flip over an object (smooth transition) 3 Answers
Camera to follow the player in the form of radius 1 Answer
Vector2 Lerp. Probably a simple solution C# 4 Answers
Flipping parent object in 2d with negative x scale causes rotation issues for children gameobjects 2 Answers