unity 手指旋转物体双手控制相机的远近
2016-05-11 15:44
549 查看
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class touchmove : MonoBehaviour {
float speed =0.1f;
public GameObject Target;
public float minDistance;
public float maxDistance;
public float scaleSmooth;
public float moveSmooth;
private float <
4000
/span>camy;
private float rotationx;
private float rotationy;
private Touch oldTouch1; //上次触摸点1(手指1)
private Touch oldTouch2;
public Camera scalCamera;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if ( Input.touchCount <= 0 ){
return;
}
if(Input.touchCount>0&&Input.GetTouch(0).phase==TouchPhase.Moved)
{
Vector2 touchDeltaPosition = Input.GetTouch (0).deltaPosition;
transform.Rotate (-touchDeltaPosition.y*speed,-touchDeltaPosition.x*speed,0);
}
ScaleModel();
}
void MoveCam()
{//单点触摸, 水平上下旋转
if( 1 == Input.touchCount ){
Touch touch = Input.GetTouch (0);
Vector2 deltaPos = touch.deltaPosition;
rotationy -= deltaPos.y;
transform.RotateAround (Target.transform.position, Vector3.up, deltaPos.y);
rotationx -= deltaPos.x;
transform.RotateAround (Target.transform.position, Vector3.right, deltaPos.x);
//transform.rotation.x = Mathf.Clamp (transform.rotation.x,0,90);
transform.LookAt (Target.transform.transform.position);
if (transform.eulerAngles.x < 45)
transform.eulerAngles = new Vector3 (45,transform.eulerAngles.y,transform.eulerAngles.z);
if (transform.eulerAngles.x > 75)
transform.eulerAngles = new Vector3 (75,transform.eulerAngles.y,transform.eulerAngles.z);
}
}
void ScaleModel()
{
//多点触摸, 放大缩小
Touch newTouch1 = Input.GetTouch (0);
Touch newTouch2 = Input.GetTouch (1);
//第2点刚开始接触屏幕, 只记录,不做处理
if( newTouch2.phase == TouchPhase.Began ){
oldTouch2 = newTouch2;
oldTouch1 = newTouch1;
return;
}
//计算老的两点距离和新的两点间距离,变大要放大模型,变小要缩放模型
float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);
//两个距离之差,为正表示放大手势, 为负表示缩小手势
float offset = newDistance - oldDistance;
scalCamera.fieldOfView += offset*scaleSmooth;
scalCamera.fieldOfView = Mathf.Clamp (scalCamera.fieldOfView,minDistance,maxDistance);
//记住最新的触摸点,下次使用
oldTouch1 = newTouch1;
oldTouch2 = newTouch2;
}
}
using System.Collections;
using UnityEngine.UI;
public class touchmove : MonoBehaviour {
float speed =0.1f;
public GameObject Target;
public float minDistance;
public float maxDistance;
public float scaleSmooth;
public float moveSmooth;
private float <
4000
/span>camy;
private float rotationx;
private float rotationy;
private Touch oldTouch1; //上次触摸点1(手指1)
private Touch oldTouch2;
public Camera scalCamera;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if ( Input.touchCount <= 0 ){
return;
}
if(Input.touchCount>0&&Input.GetTouch(0).phase==TouchPhase.Moved)
{
Vector2 touchDeltaPosition = Input.GetTouch (0).deltaPosition;
transform.Rotate (-touchDeltaPosition.y*speed,-touchDeltaPosition.x*speed,0);
}
ScaleModel();
}
void MoveCam()
{//单点触摸, 水平上下旋转
if( 1 == Input.touchCount ){
Touch touch = Input.GetTouch (0);
Vector2 deltaPos = touch.deltaPosition;
rotationy -= deltaPos.y;
transform.RotateAround (Target.transform.position, Vector3.up, deltaPos.y);
rotationx -= deltaPos.x;
transform.RotateAround (Target.transform.position, Vector3.right, deltaPos.x);
//transform.rotation.x = Mathf.Clamp (transform.rotation.x,0,90);
transform.LookAt (Target.transform.transform.position);
if (transform.eulerAngles.x < 45)
transform.eulerAngles = new Vector3 (45,transform.eulerAngles.y,transform.eulerAngles.z);
if (transform.eulerAngles.x > 75)
transform.eulerAngles = new Vector3 (75,transform.eulerAngles.y,transform.eulerAngles.z);
}
}
void ScaleModel()
{
//多点触摸, 放大缩小
Touch newTouch1 = Input.GetTouch (0);
Touch newTouch2 = Input.GetTouch (1);
//第2点刚开始接触屏幕, 只记录,不做处理
if( newTouch2.phase == TouchPhase.Began ){
oldTouch2 = newTouch2;
oldTouch1 = newTouch1;
return;
}
//计算老的两点距离和新的两点间距离,变大要放大模型,变小要缩放模型
float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);
//两个距离之差,为正表示放大手势, 为负表示缩小手势
float offset = newDistance - oldDistance;
scalCamera.fieldOfView += offset*scaleSmooth;
scalCamera.fieldOfView = Mathf.Clamp (scalCamera.fieldOfView,minDistance,maxDistance);
//记住最新的触摸点,下次使用
oldTouch1 = newTouch1;
oldTouch2 = newTouch2;
}
}
相关文章推荐
- (英文)关于Unity3d c#和.a\.o\.so等native库文件的交互,库文件可以由c\c++\object-c等编译
- Unity Shader学习2——第一个Shader程序解析
- unity 通过触摸屏幕产生射线实例化玩家
- unity 按下鼠标左键随机生成物体Instantiate
- unity 通过键盘控制角色的移动和旋转
- Part2:Unity学习笔记十二 - Space Shooter(从视频最后一课向Done_Main.unity场景修改的过程)
- unity 根据鼠标的移动来旋转物体Rotate
- unity 相机平滑的接近物体脚本 Mathf.Smooth
- unity 用鼠标控制相机绕着物体旋转脚本
- Unity中的Path对应各平台中的Path
- unity 在移动平台中,文件操作路径详解
- 浅谈unity3d中使用MVC框架模式
- Android Studio 2.1 和 Unity3D 5.3.4 交互(二)
- Unity3D中uGUI事件系统简述及使用方法总结
- Unity3.X 笔记01
- 旋转角色
- Unity3d Realtime Dynamic Clouds Rendering
- Unity Shaders and Effects Cookbook (7-1) 在Surface Shader 中 访问 顶点颜色
- unity 的内存管理
- unity+Cardboard SDK VR开发Cardboard Unity SDK讲解