- Home /
How do I rotate Z axis based off mouse click and drag?
I am attempting to rotate an object and it is just spinning out of control. I know it boils down to something with my math not being correct but I'm still trying to wrap my head around the whole Vector/Quaternion thing.
Any ideas as to why it is spinning so out of control. Also any suggestions on good resources to better understand Vectors and Quaternion?
public class SpinWithMouse : MonoBehaviour
{
public Transform target;
public float speed = 1f;
Transform mTrans;
Vector3 startPoint;
void Start ()
{
mTrans = transform;
}
void OnPress()
{
Vector3 currentLoc = UICamera.currentTouch.pos;
startPoint = (currentLoc - transform.localPosition);
}
void OnDrag (Vector2 delta)
{
Vector3 newLoc = UICamera.currentTouch.pos;
Vector3 targetPoint = (newLoc - transform.localPosition);
Quaternion targetRotation = Quaternion.LookRotation(newLoc - startPoint);
if (target != null)
{
target.localRotation = Quaternion.Slerp(target.localRotation, targetRotation, speed) * target.localRotation;
}
else
{
mTrans.localRotation = Quaternion.Slerp(mTrans.localRotation, targetRotation, speed) * mTrans.localRotation;
}
}
}
Anyone have any suggestions or thoughts? I've tried a number of different things and I just can't seem to get this working correctly. I know it has to be something with my math, but I can't wrap my head around where the problem is.
Answer by whydoidoit · Oct 05, 2012 at 02:54 PM
You are multiplying speed by Time.time in the drag - that's not a good idea. Slerp wants a number between 0 and 1, but Time.time is the number of seconds since the game started.
Your best choice would be to replace that with something like speed/10 which will incrementally move the rotation towards the target each frame by a percentage of the difference which will give a smoothed effect.
Ok, I changed the code some and I got the crazy spining under control however I need it to rotate on the Z-axis.
using UnityEngine;
public class SpinWith$$anonymous$$ouse : $$anonymous$$onoBehaviour { public Transform target; public float speed = 1f;
Transform mTrans;
Vector3 startPoint;
void Start ()
{
mTrans = transform;
}
void OnPress()
{
Vector3 currentLoc = UICamera.currentTouch.pos;
startPoint = (currentLoc - transform.localPosition);
}
void OnDrag (Vector2 delta)
{
Vector3 newLoc = UICamera.currentTouch.pos;
Vector3 targetPoint = (newLoc - transform.localPosition);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.localPosition);
if (target != null)
{
target.localRotation = Quaternion.Slerp(target.localRotation, targetRotation, speed);
}
else
{
mTrans.localRotation = Quaternion.Slerp(mTrans.localRotation, targetRotation, speed);
}
}
}
Your answer
Follow this Question
Related Questions
Rotation Jumping values (0 to 180) 1 Answer
How can I move an object to click point in 2D? 0 Answers
Why Rotate player toward mouse direction keeps increasing rotation speed? 1 Answer
How can you make a nav mesh agent rotate 180 degrees? 1 Answer
When applying a 90 degree rotation to Euler Angles, it is over/undershooting sometimes.. 2 Answers