using
UnityEngine;
using
System.Collections;
public
class
Move : MonoBehaviour
{
public
Transform target;
private
float
distance = 10.0f;
private
float
xSpeed = 250.0f;
private
float
ySpeed = 120.0f;
private
float
yMinLimit = -20;
private
float
yMaxLimit = 80;
private
float
x = 0.0f;
private
float
y = 0.0f;
private
Vector2 oldPosition1 =
new
Vector2 (0, 0);
private
Vector2 oldPosition2 =
new
Vector2 (0, 0);
void
Start ()
{
Debug.Log (oldPosition1);
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
GetComponent<Rigidbody> ().freezeRotation =
true
;
}
void
Update ()
{
if
(Input.touchCount == 1) {
if
(Input.GetTouch (0).phase == TouchPhase.Moved) {
x += Input.GetAxis (
"Mouse X"
) * xSpeed * 0.02f;
y -= Input.GetAxis (
"Mouse Y"
) * ySpeed * 0.02f;
}
}
if
(Input.touchCount > 1) {
if
(Input.GetTouch (0).phase == TouchPhase.Moved || Input.GetTouch (1).phase == TouchPhase.Moved) {
var tempPosition1 = Input.GetTouch (0).position;
var tempPosition2 = Input.GetTouch (1).position;
if
(isEnlarge (oldPosition1, oldPosition2, tempPosition1, tempPosition2)) {
if
(distance > 3) {
distance -= 0.5f;
}
}
else
{
if
(distance < 18.5f) {
distance += 0.5f;
}
}
oldPosition1 = tempPosition1;
oldPosition2 = tempPosition2;
}
}
}
bool
isEnlarge (Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
{
float
leng1 = Mathf.Sqrt ((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
float
leng2 = Mathf.Sqrt ((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
if
(leng1 < leng2) {
return
true
;
}
else
{
return
false
;
}
}
void
LateUpdate ()
{
if
(target) {
y = ClampAngle (y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler (y, x, 0);
Vector3 position = rotation *
new
Vector3 (0.0f, 0.0f, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
static
float
ClampAngle (
float
angle,
float
min,
float
max)
{
if
(angle < -360)
angle += 360;
if
(angle > 360)
angle -= 360;
return
Mathf.Clamp (angle, min, max);
}
}