在项目制作过程中,我们可能会用到镜头的拉伸与旋转操作,不管在pc端还是移动端,拉伸效果要么就是放大target,要么就是移动camera,旋转也是一样,要么旋转摄像机,要么是旋转物体本身,具体情况具体对待。下面就两个方法的具体实现方法:
下面是通过控制camera与target的距离,实时更新camera的位置来实现物体的方法缩小,也就是拉伸效果。同样旋转物体也是通过setpos方法,实时更新位置实现摄像机的旋转。这种拉伸效果适合用在target是三维模型时。
using UnityEngine;
using System.Collections;
public class RotateAndPinch_2 : MonoBehaviour
{
public Transform target;
private float minVertical = 0f;
private float maxVertical = 85f;
private float x = 0.0f;
private float y = 0.0f;
private float distance = 0.0f;
private float newdis = 0;
private float olddis = 0;
// Use this for initialization
void Start()
{
distance = (transform.position - target.position).magnitude;
}
// Update is called once per frame
void Update()
{
transform.LookAt(target);
float dt = Time.deltaTime;
x = transform.eulerAngles.y;
y = transform.eulerAngles.x;
if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
{
if (Input.touchCount == 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
float x1 = Input.GetAxis("Mouse X");
float y1 = Input.GetAxis("Mouse Y");
x += x1 * dt * 15