So I’ve been playing with this for several weeks and I’ve tried several iterations of code and some have gotten me close, but not exactly where I want to be.
Basically, I am trying to rotate a wheel, in 2D space, in a circle toward the direction of where the mouse is currently located at. I think the place where I am getting stuck is how to make the rotation actually occur correctly. As it is currently, when the mouse is pressed on the object I record the location of the click. Then as I drag the mouse around I record the location of the mouse.
I’ve tried getting the angle and setting Euler, getting the angle and use RotateAround, using LookRotation, and FromToRotation. The closest I’ve come is with FromToRotation which I’ll post below. LookRotation works correctly for the rotation, but it flips the object so it is parallel to the z-axis and I need it to stay perpendicular to the z-axis.
I have been trying to get this to work for several weeks and I know this isn’t impossible, I just can’t figure out how to get it to work. Any help would be greatly appreciated!!! See below for examples.
Here is what I am trying to accomplish in a simple diagram:
Here is the FromToRotation code, that works pretty well, except that I have to drag in a really large out circle in order to get it to rotate correctly.
using UnityEngine;
public class SpinWithMouse : MonoBehaviour
{
public Transform target;
public float speed = 1f;
Transform mTrans;
Vector3 currentLoc;
public float angle = 0f;
void Start ()
{
//Set the transform for the script to the transform of the object the script is attached to
mTrans = transform;
}
void OnPress()
{
//Set the x,y,z that is currently clicked or touched
currentLoc = UICamera.currentTouch.pos;
}
void OnDrag (Vector2 delta)
{
//Set the x,y,z of where the mouse is currently at
Vector3 newLoc = UICamera.currentTouch.pos;
//Get the difference between the start angle and the stop angle
//Quaternion targetRotation = Quaternion.LookRotation(newLoc - currentLoc);
//Set the rotation of the object based on where the click occured and where the mouse is dragged i
mTrans.localRotation = Quaternion.FromToRotation(currentLoc,newLoc); //Quaternion.Euler(0f, 0f, -0.5f * delta.x * speed) * mTrans.localRotation;
Debug.Log("Position " + UICamera.currentTouch.pos);
Debug.Log("Wheel location " + mTrans.localRotation);
}
}
Then this is LookRotation Code which gets the rotation perfectly, but it transforms the object so it’s no longer laying flat and thus defeats what I am trying to accomplish.
using UnityEngine;
public class SpinWithMouse : MonoBehaviour
{
public Transform target;
public float speed = 1f;
Transform mTrans;
Vector3 currentLoc;
Vector3 startTouch;
public float angle = 0f;
void Start ()
{
//Set the transform for the script to the transform of the object the script is attached to
mTrans = transform;
}
void OnPress()
{
//Set the x,ythat is currently clicked or touched
currentLoc = UICamera.currentTouch.pos;
//Set the distance of the touch from the object
startTouch = currentLoc - mTrans.localPosition;
}
void OnDrag (Vector2 delta)
{
//Set the x,y of where the mouse is currently at
Vector3 newLoc = UICamera.currentTouch.pos;
Vector3 currentTouch = newLoc - mTrans.localPosition;
//Get the difference between the start angle and the stop angle
//Quaternion targetRotation = Quaternion.LookRotation(newLoc - currentLoc);
//Set the rotation of the object based on where the click occured and where the mouse is dragged i
Quaternion newRotation = Quaternion.LookRotation(newLoc - currentLoc); //Quaternion.FromToRotation(startTouch,currentTouch); //Quaternion.Euler(0f, 0f, -0.5f * delta.x * speed) * mTrans.localRotation;
mTrans.localRotation = newRotation;
Debug.Log("Position " + UICamera.currentTouch.pos);
Debug.Log("Wheel location " + mTrans.localRotation);
}
}
This is my code trying to use angles, but it just rotates out of control with no seeming ryhm or reason.
using UnityEngine;
public class SpinWithMouse : MonoBehaviour
{
public Transform target;
public float speed = 1f;
Transform mTrans;
Vector3 currentLoc;
Vector3 startTouch;
public float angle = 0f;
void Start ()
{
//Set the transform for the script to the transform of the object the script is attached to
mTrans = transform;
}
void OnPress()
{
//Set the x,y that is first clicked or touched
currentLoc = UICamera.currentTouch.pos;
//Set the distance of the first touch from the object
startTouch = currentLoc - mTrans.localPosition;
}
void OnDrag (Vector2 delta)
{
//Set the x,y of where the mouse or touch is currently at
Vector3 newLoc = UICamera.currentTouch.pos;
//Set the distance of the current touch from the object
Vector3 currentTouch = newLoc - mTrans.localPosition;
//Get the Angle between the starting location and the current location
float angle = Vector3.Angle(newLoc, currentLoc);
//Set the rotation of the object based on where the click occured and where the mouse is dragged i
//Quaternion newRotation = Quaternion.LookRotation(newLoc - currentLoc); //Quaternion.FromToRotation(startTouch,currentTouch); //Quaternion.Euler(0f, 0f, -0.5f * delta.x * speed) * mTrans.localRotation;
mTrans.RotateAround(transform.forward, angle);
Debug.Log("Position " + UICamera.currentTouch.pos);
Debug.Log("Wheel location " + mTrans.localRotation);
}
}