Hi.
I need help figuring out how to rotate a object on mouse drag but to make it so that the object starts rotating from its original position but not from the mouse click position. My scene uses an orthographic camera that is set to z:-10 and
Iv’e pretty much got the general rotation part figured out and working.
Here’s the code I’m using:
using UnityEngine;
using System.Collections;
public class TestScript : MonoBehaviour {
Camera myCam;
private Vector3 mousePos;
private Vector3 mousePosOnMouseDown;
private Vector3 clickOffset;
// Use this for initialization
void Start () {
myCam=Camera.main;
}
void Update () {
mousePos = myCam.ScreenToWorldPoint(Input.mousePosition);
//This fires only on the frame the button is clicked
if(Input.GetMouseButtonDown(0))
{
mousePosOnMouseDown = new Vector3(mousePos.x,mousePos.y,transform.position.z);
clickOffset = transform.position-mousePosOnMouseDown;
}
//This fires while the button is pressed down
if(Input.GetMouseButton(0))
{
Vector3 norm = mousePos-transform.position;
float angle = Mathf.Atan2(norm.x, norm.y) * Mathf.Rad2Deg;
transform.eulerAngles = new Vector3(angle,90,90);
}
}
}
And here’s a short clip of it working :[https://docs.google.com/file/d/0B0ozmyp9_wwiQm4zd0xFR2xjVVU/edit?usp=sharing][1]
[1]: https://docs.google.com/file/d/0B0ozmyp9_wwiQm4zd0xFR2xjVVU/edit?usp=sharing
If you look at the video you can see the object snapping to the point where I clicked. And right now it should, because I’m not actually doing anything to prevent that in my code. What I would like to achieve is: when I click on the mouse button the object keeps it’s rotation but when I start dragging the mouse, then the object starts rotating relative to the direction and amount i have dragged.
It should be quite simple, at least that’s what I thought 3 days ago, and before running out of different ideas how to even search for a solution.
So if anybody has an idea how to make this happen, then please let me know