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 MSachs · Aug 12, 2019 at 05:28 AM · rotationsmoothmousedragdamping

Rotate something smoothly by dragging?

Hi,

I am rotating a GameObject OnMouseDrag() using the following script.


void OnMouseDrag() { float mousePosX = Input.GetAxis("Mouse X") * rotationSpeed; transform.Rotate(Vector3.back, -mousePosX, Space.Self); }


I would like to rotate it smoothly so it doesn't come to an abrupt halt as soon as you are not dragging anymore but instead decreases its rotation over time when you stop dragging.

Any help on how to achieve this would be much appreciated.


Thanks in advance

Comment
Add comment · Show 2
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 ShadyProductions · Aug 12, 2019 at 08:59 AM 0
Share

You can use Lerp or Slerp to slowly rotate

avatar image MSachs ShadyProductions · Aug 12, 2019 at 09:20 AM 0
Share

But I would have to change up the script, right? Or is it possible to incorporate it into the existing one using transform.Rotate?

2 Replies

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

Answer by Hellium · Aug 12, 2019 at 11:20 AM

Get rid of the OnMouseDrag function, use the following script, and don't forget to add an EventSystem in your scene (Create > UI > EventSystem), and to attach a raycaster to your camera ( GraphicRaycaster if you use image, PhysicsRaycaster if you use 3D objects, Physics2DRaycaster if you use sprites)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class YourScript : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
 {
     public float rotationSpeed;
     public float rotationDamping;
 
     private float _rotationVelocity;
     private bool _dragged;
 
     public void OnBeginDrag(PointerEventData eventData)
     {
         _dragged = true;
     }
 
     public void OnDrag(PointerEventData eventData)
     {
         _rotationVelocity = eventData.delta.x * rotationSpeed;
         transform.Rotate(Vector3.back, -_rotationVelocity, Space.Self);
     }
 
     public void OnEndDrag(PointerEventData eventData)
     {
         _dragged = false;
     }
 
     private void Update()
     {
         if( !_dragged && !Mathf.Approximately( _rotationVelocity, 0 ) )
         {
             float deltaVelocity = Mathf.Min(
                 Mathf.Sign(_rotationVelocity) * Time.deltaTime * rotationDamping,
                 Mathf.Sign(_rotationVelocity) * _rotationVelocity
             );
             _rotationVelocity -= deltaVelocity;
             transform.Rotate(Vector3.back, -_rotationVelocity, Space.Self);
         }
     }
 }


Comment
Add comment · Show 12 · 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 MSachs · Aug 12, 2019 at 11:53 AM 0
Share

Thanks a lot! It works nicely but somehow @Propagant solution using On$$anonymous$$ouseDrag looks a little better and smoother no matter how I change the values in your script. Is there a specific reason why I should get rid of On$$anonymous$$ouseDrag and use yours?

avatar image Hellium MSachs · Aug 12, 2019 at 12:13 PM 1
Share

$$anonymous$$y answer uses the EventSystem ins$$anonymous$$d of the (legacy) On$$anonymous$$ouseXXX function of Unity.

But if Propagant's answer fits your needs, that's perfect ;)

avatar image MSachs Hellium · Aug 12, 2019 at 12:19 PM 0
Share

O$$anonymous$$, got it! Thanks a lot anyway :)

Show more comments
avatar image
0

Answer by Propagant · Aug 12, 2019 at 11:18 AM

Hey, try this one:

     public float mouseSpeedMultiplier = 8;
     public float smoothSpeed = 0.04f;

     private float mouseX;

     void OnMouseDrag()
     {
         mouseX += Input.GetAxis("Mouse X") * mouseSpeedMultiplier;
     }
 
     void LateUpdate() //Cause we are using Lerp function
     {
         transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, -mouseX, 0), smoothSpeed);
     }

You can also add Y or Z rotation in Quaternion-Euler parameters. Hope that helped!

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 MSachs · Aug 12, 2019 at 11:50 AM 1
Share

Thanks a lot! Works fantastically! :)

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

146 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 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

Smooth tilting rotation on slopes like cars? 1 Answer

Quaternion Rotation Smooth 1 Answer

Lerp Rotation in World Space 1 Answer

Smooth open/close door script coroutine issue 1 Answer

move based on camera position 0 Answers


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