- Home /
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
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;
}
}
}
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
Add this at the top of update
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
mouseX = Input.mousePosition.x;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612191424im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
crouch Uncrouch speed with animation curve 1 Answer
interpolate a move variable 0 Answers
animation length, changing speeds quandary 0 Answers
Lerp - Time Remaining 1 Answer