- Home /
Drag to Rotate using Arcball theory ( 3 axis )
Im trying to rotate a 3D object using the arcball theory, so far i've found one that does what i wanted, BUT it uses transform.rotateAround that constantly applies angular velocity everytime i swipe (still holding down my fingers), what i wanted is to use transform.Rotate , and i want to be able to "Drag to Rotate" the object. meaning if i dont move my finger, the object should'nt move.
I tweaked the code so i get what i wanted, but even i know that it isn't efficient or even effective.
What i did = i added a variable delta, which is the delta of my fingers location every frame. Meaning it can calculate how fast im moving my fingers. Then i applied the delta on the angularvelocity, so when my finger moves faster the object rotates faster, but when i stop moving my fingers, the object angularvelocity is 0.
i know the code is very barbaric but i really cant figure out any other way, maybe it's because i dont have deep comprehension on quaternions and other function relating to this purpose. ive spent days trying to solve this, maybe someone can shed some light to this question using transform.Rotate ? much appreciated.
using UnityEngine;
using System.Collections;
public class Rotate_Swipe : MonoBehaviour {
private float damping = 0.9f;
private static float speed = 2;
private Vector3 delta = Vector3.zero;
private Vector3 lastPos = Vector3.zero;
private Vector3 vDown;
private Vector3 vDrag;
private bool dragging;
private float angularVelocity;
private Vector3 rotationAxis;
void Start ()
{
dragging = false;
angularVelocity = 0;
rotationAxis = Vector3.zero;
}
void Update ()
{
if (Input.touchCount == 1) {
Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
if (!dragging) {
//extract vDown from the RaycastHit
vDown = hit.point - transform.position;
dragging = true;
} else {
// extract vDrag from the RaycastHit
delta = hit.point - lastPos;
vDrag = hit.point - transform.position;
// compute the rotation axis and angular velocity from vDown and vDrag
rotationAxis = Vector3.Cross (vDown, vDrag);
angularVelocity = Vector3.Angle (vDown, vDrag) * (Mathf.Abs(delta.magnitude)+speed);
vDown = hit.point - transform.position;
lastPos = hit.point;
}
else
dragging = false;
}
}
if (Input.GetMouseButtonUp (0) || Input.touchCount != 1)
dragging = false;
// apply the angular velocity
transform.RotateAround (rotationAxis, angularVelocity * Time.deltaTime);
angularVelocity = (angularVelocity > 0.01f) ? angularVelocity * damping : 0;
}
Does this code work? I got two errors right off the bat:
Assets/ArcBall.cs.cs(46,36): error CS1525: Unexpected symbol `else'
and:
Assets/ArcBall.cs.cs(61,1): error CS8025: Parsing error
It's missing a second curly brace before the else on line 46 I believe.
Your answer
Follow this Question
Related Questions
Transform.rotation vs Wrapping 1 Answer
RotateAround object in local space 1 Answer
Rotate Issue 0 Answers
Rotating game Object. 2 Answers
Rotate Object Around Local X Axis , then Local Y Axis, then Local Z Axis (like a turtle) 0 Answers