Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Brokep · Oct 17, 2015 at 08:23 PM · c#rotationmousedragcircular

Drag and rotate object similarly to Monument Valley rotation mechanics?

Hello everybody,

I've been trying to get an object to rotate just as the drag event works in Monument Valley. Here's an example (skip to 0:13) youtube vid

Also if you happen to own Monument Valley, check Chapter 7 ("The Rookery"), that way you will instantly understand the type of rotation-movement I want to achieve.

So far I've tried a lot of things I found on other people's questions & topics but just can't get it to work right. Also I'm trying to get it work with the Void OnMouseDrag (). Asking for help here is literally the last thing I have left.

Not sure what else I should say regarding this, my so far Unity knowledge isn't enough to figure this out by my own. Here's also the code I last tried. If you feel like helping me then thank you in advance.

Edit: Google Drive link did a quick project, try to rotate the object from bottom->Right->Top, it will stop halfway and then rotate backwards. If you constantly drag on the X axis it will keep rotating though which is also wrong. On MV, it works just fine when you do a full circle.

Hopefully the last edit: Here's the final script Pastebin Link

 using UnityEngine;
 using System.Collections;
  
 public class DragRotateX : MonoBehaviour
 {
  
     public int speed;
     public float friction;
     public float xDeg;
     public float yDeg;
     public float zDeg;
     Quaternion fromRotation;
     Quaternion toRotation;
  
   void OnMouseDrag()
      {
           yDeg += Input.GetAxis ("Mouse Y") * speed * friction;
           xDeg -= Input.GetAxis ("Mouse X") * (Mathf.Cos (yDeg/60)*speed);
           transform.eulerAngles = new Vector3(yDeg,xDeg,zDeg);     
      }
   
 }




  
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 PictonicStudio · Oct 17, 2015 at 08:30 PM 0
Share

The Script works for me, so I'm not sure what you are wanting.

avatar image Brokep PictonicStudio · Oct 17, 2015 at 08:49 PM 0
Share

Google Drive link did a quick project, try to rotate the object from bottom->Right->Top, it will stop halfway and then rotate backwards. If you constantly drag on the X axis it will keep rotating though. Which is also wrong. On $$anonymous$$V, it works just fine when you do a full circle.

2 Replies

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

Answer by PictonicStudio · Oct 17, 2015 at 09:32 PM

Try this script instead :)

     private Quaternion originalRotation;
     private float startAngle = 0;
 
     public void Start()
     {
         originalRotation = this.transform.rotation;
     }
     public void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             MouseDown();
         }
         if (Input.GetMouseButton(0))
         {
             MouseHeld();
         }
     }
 
     public void MouseDown()
     {
         originalRotation = this.transform.rotation;
         Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
         Vector3 vector = Input.mousePosition - screenPos;
         startAngle = Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg;
     }
 
     public void MouseHeld()
     {
         Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
         Vector3 vector = Input.mousePosition - screenPos;
         float angle = Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg;
         Quaternion newRotation = Quaternion.AngleAxis(angle - startAngle, this.transform.up);
         newRotation.z = 0; 
         newRotation.eulerAngles = new Vector3(0, -newRotation.eulerAngles.y, 0);
         this.transform.rotation = originalRotation * (newRotation);
     }

Comment
Add comment · Show 5 · 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 Brokep · Oct 17, 2015 at 09:44 PM 0
Share

That works great. Thank you a lot!

avatar image Brokep · Oct 17, 2015 at 11:54 PM 0
Share

Hey there buddy, you already helped me enough so if you're bored just let it go but if you could help me with one last thing it would be really great and I'd be pretty much done with this silly rotation animation. Not sure if you have the game downloaded but once you rotate your object inside $$anonymous$$onument Valley then it rounds up to 90/180/270/360 and so on with a nice smooth transition towards that angle. Figured of using something simple like the code below, which works okay but it just looks really lame that it instantly rounds up the number, would it be possible to rotate towards 90/180/270/360 slowly like it's been dragged back so it seems more realistic like in $$anonymous$$V?

 void On$$anonymous$$ouseUp() {
             var vec = transform.eulerAngles;
             vec.x = $$anonymous$$athf.Round(vec.x / 90) * 90;
             vec.y = $$anonymous$$athf.Round(vec.y / 90) * 90;
             vec.z = $$anonymous$$athf.Round(vec.z / 90) * 90;
             transform.eulerAngles = vec;
 }

avatar image PictonicStudio Brokep · Oct 18, 2015 at 12:49 AM 0
Share

You could quantize the rotation, and then lerp to the new rotation

avatar image PictonicStudio Brokep · Oct 18, 2015 at 03:22 AM 0
Share

Here. I included a script with a modified example scene from the one you sent me. Hope it works!

https://drive.google.com/file/d/0B0z$$anonymous$$duoZHgxBUy1G$$anonymous$$Wh2Q1A3d3c/view?usp=sharing

avatar image Brokep PictonicStudio · Oct 20, 2015 at 01:11 PM 1
Share

Thank you a lot for helping me out, there was a little issue I couldn't sort out and long story short I've found another way which works equally great. Thought you might be interested to know since you helped me out in the first place.

Pastebin Link

avatar image
0

Answer by Geoffypop · Apr 08, 2016 at 11:37 PM

Hey @PictonicSudio, I'd like to use your script but the math is beyond me, if I were to attempt to alter the axis that it operates on (i.e. to x or z) what would I change in this script? Furthermore, is it possible to make it work even when the object is viewed from the "bottom" (opposite side) without generating the rotation in opposite direction?

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 yapaysinek · Apr 21, 2016 at 08:44 AM 0
Share

Actually I was looking for the same thing. If someone could help me to make this work on the y axis as well that would be great.

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

35 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

Related Questions

Help with weapon rotation top down 2D C# 0 Answers

Problem with rotation to mouse position 0 Answers

Touch to move and drag to rotate object 2 Answers

Rotate towards mouse pointer 1 Answer

OnMouseDrag() stopped working after few clicks 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