Unity旋转.缩放和平移物体
一.Unity旋转.缩放和平移物体的方式有两种
1.是通过控制相机来达到看起来物体的旋转和缩放,这种方式的效果就像人站在一个位置转身,眼睛所看到的景色也会改变,人朝一个物体方向走近,物体会近大远小。像室内展示.汽车展示和VR等会经常用到这种方式
2.另外一种方式是通过控制物体自身,来达到旋转.缩放和平移,这种方式是对单个物体操作。今天我们讲的就是这种方式
二.脚本属性介绍
1.rotateXSpeed 控制水平旋转速度
2.rotateYSpeed 控制垂直旋转速度
3.zoomMini 控制缩放最小值
4.zoomMax 控制缩放最大值
三.脚本的使用
1.新建一个C#脚本命名为“RotatingZoomObject”,脚本我后面会贴出来
2.新建一个空对象命名为“ModelController”,挂载“RotatingZoomObject”脚本
3.新建一个空对象命名为“Horizontal”,将它拖到“ModelController”层级下,位置.旋转.大小归零
4.新建一个空对象命名为“Vertical”,将它拖到“Horizontal”层级下,位置.旋转.大小归零
5.新建一个空对象命名为“Model”,将它拖到“Vertical”层级下,位置.旋转.大小归零
6.将要控制的模型移动道“Model”层级下
7.PC平台只能控制旋转和缩放,如果想控制移动,修改下下面脚本就可以
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
/// <summary>
/// 模型旋转.缩放和平移物体
/// </summary>
public class RotatingZoomObject : MonoBehaviour
{
#region 数据申明
/// <summary>水平旋转速度</summary>
private float rotateXSpeed = 100;
/// <summary>垂直旋转速度</summary>
private float rotateYSpeed = 100;
/// <summary>缩放最小值</summary>
private float zoomMini = 0.5f;
/// <summary>缩放最大值</summary>
private float zoomMax = 2.5f;
private float mouseX = 0;
private float mouseY = 0;
/// <summary>控制水平旋转</summary>
private Transform rotateHorizontal;
/// <summary>控制垂直旋转</summary>
private Transform rotateVertical;
private Touch oldTouch1;
private Touch oldTouch2;
private GameObject modelCamera;
#endregion
#region Unity函数
private void Awake()
{
rotateHorizontal = transform.Find("Horizontal");
rotateVertical = transform.Find("Horizontal/Vertical");
modelCamera = GameObject.Find("ModelCamera").gameObject;
}
private void Update()
{
switch(Application.platform)
{
case RuntimePlatform.Android:
case RuntimePlatform.IPhonePlayer:
if(Input.touchCount>0)
{
if (EventSystem.current != null)
{
if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) return;
}
if (Input.touchCount == 1) RotateModelForMobile();
if (Input.touchCount >= 2)
{
Touch newTouch1 = Input.GetTouch(0);
Touch newTouch2 = Input.GetTouch(1);
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;
float res= (offset > 0)? offset : -offset;
ZoomModelForMobile(offset);
//Debug.Log("手指距离:"+res);
//缩放
if(res>150)
{
ZoomModelForMobile(offset);
//DebugManager.Log("缩放:" + res);
}
//移动
else
{
transform.localPosition += new Vector3(newTouch1.deltaPosition.x/300,newTouch1.deltaPosition.y/300,0);
//DebugManager.Log("移动:" + res);
}
oldTouch1 = newTouch1;
oldTouch2 = newTouch2;
}
}
break;
default:
if (EventSystem.current != null)
{
if (EventSystem.current.IsPointerOverGameObject()) return;
}
RotateModelForPc();
ZoomModelForPc();
break;
}
}
#endregion
#region 外部接口
/// <summary>重置</summary>
public void Reset()
{
transform.localPosition = new Vector3(0, 0f, 5f);
transform.localScale = Vector3.one;
rotateHorizontal.transform.localEulerAngles = Vector3.zero;
rotateVertical.transform.localEulerAngles = Vector3.zero;
if(modelCamera!=null)
{
modelCamera.transform.localPosition = Vector3.zero;
modelCamera.transform.localEulerAngles = Vector3.zero;
modelCamera.transform.localScale = Vector3.zero;
}
}
#endregion
#region 旋转模型
/// <summary>PC平台旋转模型</summary>
private void RotateModelForPc()
{
if (Input.GetMouseButtonDown(0))
{
mouseX = 0;
mouseY = 0;
}
else if (Input.GetMouseButton(0))
{
mouseX = Input.GetAxis("Mouse X");
mouseY = Input.GetAxis("Mouse Y");
float x = mouseX > 0 ? mouseX : -mouseX;
float y = mouseY > 0 ? mouseY : -mouseY;
if (mouseX > 0) mouseX = -mouseX;
else mouseX = -mouseX;
if (x > y) rotateVertical.Rotate(0, mouseX * Time.deltaTime * rotateXSpeed, 0);
else rotateHorizontal.Rotate( mouseY * Time.deltaTime * rotateYSpeed,0,0);
//if (x > y) rotateHorizontal.Rotate(0, mouseX * Time.deltaTime * rotateXSpeed, 0);
//else rotateVertical.Rotate(0, 0,mouseY * Time.deltaTime * rotateYSpeed);
}
}
/// <summary>移动平台模型旋转</summary>
private void RotateModelForMobile()
{
Touch touch = Input.GetTouch(0);
mouseX = touch.deltaPosition.x;
mouseY = touch.deltaPosition.y;
float x = mouseX > 0 ? mouseX : -mouseX;
float y = mouseY > 0 ? mouseY : -mouseY;
if (mouseX > 0) mouseX = -mouseX;
else mouseX = -mouseX;
if (x > y) rotateVertical.Rotate(0, mouseX * Time.deltaTime * (rotateXSpeed / 20), 0);
else rotateHorizontal.Rotate(mouseY * Time.deltaTime * (rotateYSpeed / 20), 0, 0);
//if (x > y) rotateHorizontal.Rotate(0, mouseX * Time.deltaTime * (rotateXSpeed / 20), 0);
//else rotateVertical.Rotate(mouseY * Time.deltaTime * (rotateYSpeed / 20),0, 0);
}
#endregion
#region 缩放模型
/// <summary>Pc平台缩放模型</summary>
private void ZoomModelForPc()
{
float scaleFactor = Input.GetAxis("Mouse ScrollWheel");
SetZoom(scaleFactor);
}
/// <summary>移动平台缩放模型</summary>
private void ZoomModelForMobile(float offset)
{
float scaleFactor = offset / 350;
SetZoom(scaleFactor);
}
/// <summary>
/// 设置缩放
/// </summary>
/// <param name="value">缩放速率</param>
private void SetZoom(float value)
{
Vector3 localScale = transform.localScale;
Vector3 scale = new Vector3(localScale.x + value, localScale.y + value, localScale.z + value);
if (scale.x > zoomMini && scale.y > zoomMini && scale.z > zoomMini)
{
transform.localScale = new Vector3(Mathf.Clamp(scale.x, zoomMini, zoomMax),Mathf.Clamp(scale.y, zoomMini, zoomMax), Mathf.Clamp(scale.z, zoomMini, zoomMax));
}
}
#endregion
}
Unity QQ交流群:299412191 欢迎对Unity感兴趣的同学加入.