Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
avatar image
0
Question by exveln · Feb 09, 2019 at 01:23 PM · lerpspeedmousedrag

How to make a Smooth speed reduction after MouseDrag?

I'm new. Please help me with this task. What am I doing wrong? I've been suffering for 5 hours. It is necessary to gradually reduce the speed of rotation of the object, after I stopped rotating it with the mouse. I used Lerp, Boolean variables, it all works. But in the end nothing works.

 public class cloudRotate : MonoBehaviour
 {
     public float rotationSpeed;
     public int mouseMultiply;
     public float lerpRate;
 
     bool mouseControll;
     bool lerpNeed; 
 
     float X; //mouse drag speed
     float t; // lerp duration
 
     private void Start()
     {
         rotationSpeed = 0.2f;
         mouseMultiply = 30;
         lerpRate = 1f;
    
         mouseControll = false;
         lerpNeed = false;
     }
   
     private void Update()
     {
 
         if ( mouseControll == false ) {
             Debug.Log(rotationSpeed);
 
             if (lerpNeed) {
                 rotationSpeed = Mathf.Lerp(X, rotationSpeed, t);
                 t += lerpRate * Time.deltaTime;
                 Debug.Log(t);
             }
 
             if (t > 1.0f)
             {
                 lerpNeed = false;
                 t = 0.0f;
             }
            
         }
 
         transform.Rotate(0, rotationSpeed, 0);
 
     }
 
 
     void OnMouseDrag()
     {
         mouseControll = true;
         X = Input.GetAxis("Mouse X") * rotationSpeed * mouseMultiply;
         transform.Rotate(new Vector3(0, -1, 0), X);
     }
 
 
     private void OnMouseUp()
     {
         mouseControll = false;
         lerpNeed = true;
         t = 0;
     }
 
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by qobion · Feb 09, 2019 at 03:42 PM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class cloudRotate : MonoBehaviour
 {
     public float sensitivity = 0.2f;
     public float acceleration = 0.2f;
     public float rotationSpeed = 20.0f;
 
     private float accelerationX;
     private float mouseX;
 
     void Update()
     {
     if (Input.GetMouseButtonDown(0))
     {
         mouseX = Input.mousePosition.x;
     }
         transform.Rotate(new Vector3(0, accelerationX - rotationSpeed * Time.deltaTime, 0), Space.World);
         accelerationX = Mathf.Lerp(accelerationX, 0, Time.deltaTime / acceleration);
         if (Input.GetMouseButton(0))
         {
             float deltaX = mouseX - Input.mousePosition.x;
             accelerationX = deltaX * sensitivity;
             mouseX = Input.mousePosition.x;
         }
     }
 }
Comment
Add comment · 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
0

Answer by exveln · Feb 09, 2019 at 04:30 PM

Thank you! Now I will try to understand how it works. There's a problem here: When you click the mouse set some value rotation and get a jump

Comment
Add comment · Show 2 · 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 qobion · Feb 09, 2019 at 05:06 PM 0
Share

Add this at the top of update

         if (Input.Get$$anonymous$$ouseButtonDown(0))
         {
             mouseX = Input.mousePosition.x;
         }

avatar image exveln qobion · Feb 09, 2019 at 06:19 PM 0
Share

It's working! Thank you!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

101 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

Related Questions

interpolate a move variable 0 Answers

animation length, changing speeds quandary 0 Answers

Lerp - Time Remaining 1 Answer

lerp in shader doesn't work as imagined? 1 Answer

Slow down in the same time over different distances 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