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);
}
}
The Script works for me, so I'm not sure what you are wanting.
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.
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);
}
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;
}
You could quantize the rotation, and then lerp to the new rotation
Here. I included a script with a modified example scene from the one you sent me. Hope it works!
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.
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?
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
Follow this Question
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