- Home /
Camera orbit around target on touch device
Hi everyone,
I am trying to have a camera rotating around a target when I drag my finger on the phone. I just grabbed a code I found so I don't really understand it, so it's pretty hard to review it ( as you might understand I am just a hobbyist coder).
When I try, the camera seems to jsut rotate around its own position when I want it to rotate around the var target.
Sorry for your expert eyes, here is the code:
var target : Transform; var distance = 20.0; var wheelSpeed = 10.0;
var xSpeed = 250.0; var ySpeed = 120.0;
var yMinLimit = -20; var yMaxLimit = 80;
var minDistance = 5.0;
var touch : Touch; var curDist = 0.0; var lastDist = 0.0; var pinchSpeed : float ; var cameraSpeed =1.0;
private var x = 0.0; private var y = 0.0;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start () {
var angles= transform.eulerAngles;
x = angles.y;
y = angles.x;
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if (target && camera) {
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
touch = Input.GetTouch(0);
x += touch.deltaPosition.x * xSpeed * 0.02f;
y -= touch.deltaPosition.y * ySpeed * 0.02f;
}
if (Input.touchCount > 1 && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved))
{
var touch1 = Input.GetTouch(0);
var touch2 = Input.GetTouch(1);
curDist = Vector2.Distance(touch1.position, touch2.position);
if(curDist > lastDist)
{
distance += Vector2.Distance(touch1.deltaPosition, touch2.deltaPosition)*pinchSpeed/10;
}
else
{
distance -= Vector2.Distance(touch1.deltaPosition, touch2.deltaPosition)*pinchSpeed/10;
}
lastDist = curDist;
}
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation= Quaternion.Euler(y, x, 0);
var vTemp = new Vector3(0.0f, 0.0f, -distance);
var position= rotation * vTemp + target.position;
transform.position = Vector3.Lerp (transform.position, position, cameraSpeed*Time.deltaTime);
transform.rotation = rotation;
}
}
static function ClampAngle (angle : float, min : float, max : float) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp (angle, min, max); }
Thanks a LOT in advance Cheers
It appears to be the standard mouse orbit script rewritten for touch. The behavior you describe is the one you would get if you did not initialize the target. $$anonymous$$ake sure that you select the camera this script is attached to in the hierarchy, then drag the target object on top of the 'Target' variable in teh Inspector.
The layout is messy,You should rearrange the code:)But the function works.Thanks for sharing.Where do you get this source code,please?
Answer by feydfeyd · Mar 19, 2013 at 07:36 AM
Thnaks a lot! I also removed some smooth parameters which were making the camera too slow. And changed the speed
working now.
Thanks a lot.