Comments and answers for "Mathf.Atan changes instantly even with a slow lerped value"
http://answers.unity.com/questions/1828136/mathfatan-changes-instantly-even-with-a-slow-lerpe.html
The latest comments and answers for the question "Mathf.Atan changes instantly even with a slow lerped value"Comment by Dracolyte on Dracolyte's answer
http://answers.unity.com/comments/1828203/view.html
Thanks for helping me, but i actually figured out myself. I had a very very very mistypen thing in the code. I only need Atan and not Atan2, because im working only with the X axis and need to rotate the player based on his direction of swipes. Interpolating between vectors worked and was the only solution for me. I very appreciate your help.Thu, 08 Apr 2021 14:52:20 GMTDracolyteAnswer by andrew-lukasik
http://answers.unity.com/answers/1828198/view.html
_
1. `startPos2.x - currentPos.x;` is not a valid input for arc tangens ( y/x is ).
2. To smoothly rotate something - you need to interpolate between angles or quaternions, not position vectors.
3. You may want to use `atan2` as it require a bit less understanding of basic trigonometry.
_
![preview][1]
using UnityEngine;
public class FabulousAdventuresInBasicTrigonometry : MonoBehaviour
{
[SerializeField] float _rotationSpeed = 11;
Vector2 mousePos = Vector2.zero;
Vector2 mouseClickPos = Vector2.zero;
Quaternion _rot = Quaternion.Euler( 0 , 0 , 0 );
void Update ()
{
mousePos = Input.mousePosition;
if( Input.GetMouseButtonDown(0) )
mouseClickPos = Input.mousePosition;
Vector2 dir = mousePos - mouseClickPos;
Quaternion targetRot = Quaternion.Euler( 0 , -Mathf.Atan2(dir.y,dir.x)*Mathf.Rad2Deg , 0 );
// Note: atan gives you counter-clockwise rotation from {y=0,x=1} (right dir vec)
// where Unity uses clockwise rotation starting at {y=1,x=0} (up dir vec)
// so compensate for that accordingly
_rot = Quaternion.Lerp( _rot , targetRot , Time.deltaTime * _rotationSpeed );
Debug.DrawLine( Vector3.zero , _rot*Vector3.right );
}
}
[1]: https://i.imgur.com/fX0BxEU.gifThu, 08 Apr 2021 14:43:40 GMTandrew-lukasik