Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Apr 08, 2021 at 02:52 PM by Dracolyte for the following reason:

Figured it finally myself out

avatar image
0
Question by Dracolyte · Apr 08, 2021 at 10:53 AM · quaternionmousepositionvector2mathfmathf.lerp

Mathf.Atan changes instantly even with a slow lerped value

I have two scripts. One is for the players x position and the other one is for the players y rotation. The position script acts like a one dimensional joystick, that goes to its original position over time. Im using Mathf.Atan(), to rotate the player based on the x axis of my "joystick". I used lerp, so the joystick goes slowly into his original position. With the lerp changed, so changes its rotation. But there is a problem. I have a currentPos (the updating mouse position) and a StartPos2 (the mouse position that sets if you click). The StartPos2 goes slowly to the position of currentPos (the distance will be 0 then). The rotation somhow changes instantly, but it should change slowly because of the lerp.

Rotation script (if i remove the * 20, value will be lower but problem doesnt go away):

 Quaternion GetQuaternion()
     {
         Quaternion rot;
         
         float angle;
 
         angle = Mathf.Atan(GetLerpedMoveDist() * Mathf.Rad2Deg / 2) * 20;
         
         rot = Quaternion.Euler(0, -angle, 0);
         
         return rot;
     } 

"Joystick" script:

 float GetLerpedMoveDist()
     {
         float lerpDist = startPos2.x - currentPos.x;
         startPos2 = Vector2.Lerp(startPos2, currentPos, Time.deltaTime * rotSmooth);
         return lerpDist;
     }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

Answer by andrew-lukasik · Apr 08, 2021 at 02:43 PM


  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

 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 );
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dracolyte · Apr 08, 2021 at 02:52 PM 0
Share

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.

Follow this Question

Answers Answers and Comments

117 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mathf.Lerp supposed to be in Update()? 2 Answers

using Mathf to find the inverse tan in degrees 1 Answer

how to use time on lerp 2 Answers

Rotate Towards doesnt work as expected 2 Answers

My Motorcycle Physics Need Some Customization 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges