Unity3D实现物体旋转缩放移动效果

更新时间:2023-05-11 06:24:23 阅读: 评论:0

Unity3D实现物体旋转缩放移动效果本⽂实例为⼤家分享了Unity3D实现物体旋转缩放移动的具体代码,供⼤家参考,具体内容如下
由于项⽬运⾏在安卓上,运⽤到了插件,⽐较⿇烦。你们可以在触发条件上进⾏修改,不⽤插件也可以。
1.下载FingerGestures 插件下载地址
2.导⼊插件,创建场景将预设Finger Gestures Initializer 拖拽到 Hierarchy 视图中
3.添加脚本,拖拽到摄像机上。创建⼀个⽅块拖拽到脚本target 属性上。
using UnityEngine;
using System.Collections;
public class ObjectControl : MonoBehaviour
{
public Transform target;
public float yawSensitivity = 80.0f;
public float pitchSensitivity = 160.0f;
public bool clampPitchAngle = true;
public float pinchZoomSensitivity = 0.5f;//缩放速度
public float smoothZoomSpeed = 10.0f;
public float smoothOrbitSpeed = 20.0f;
public float distance = 0;
float yaw = 0;
float pitch = 0;
float idealYaw = 0;
float idealPitch = 0;
float fChangeScale = 0;
float fChangeideal = 0;
public Transform[] movementP;
/// <summary>
/// 控制模式枚举
/// </summary>
public enum ControlModel
{
Zoom, Rotate, Translate
}
public ControlModel controlModel = ControlModel.Rotate;
/
/Vector3 position=new Vector3();
public bool bArrive = fal;//⿏标是否到达零件箱边界区域
//平移⽅式是否根据⿏标拖动距离还是直接置为⿏标位置
public bool ifDragMove = fal;
//平移⽅式为:根据⿏标拖动距离时,评议的速度
public float moveSpeed = 1.0f;
//是够需要画出按钮(缩放、旋转、平移)
public bool ifDrawBtn = true;
//缩放⽅式改为:改变相机范围
public bool zoomCamera = fal;
//zoomCamera = true ,相机的最⼩范围值
public float minZoom = 0f;
//zoomCamera = true ,相机的最⼤范围值
public float maxZoom = 179f;
//平移对象
public Transform moveTarget;
//平移对象的初始位置
Vector3 moveTargetPos;
//模型的直接⽗对象
public Transform parentModel;
Vector3 parentModelPos;
void Start()
{
zoomCamera = true;
}
void OnEnable()
{
FingerGestures.OnDragMove += FingerGestures_OnDragMove;
FingerGestures.OnPinchMove += FingerGestures_OnPinchMove;
FingerGestures.OnFingerDragEnd += OnFingerDragEnd;
void OnDisable()
{
FingerGestures.OnDragMove -= FingerGestures_OnDragMove;
FingerGestures.OnPinchMove -= FingerGestures_OnPinchMove;
FingerGestures.OnFingerDragEnd -= OnFingerDragEnd;
}
public void tRotation()
{
Vector3 angles = target.eulerAngles;
yaw = idealYaw = angles.y;
pitch = idealPitch = angles.x;
}
void FingerGestures_OnDragMove(Vector2 fingerPos, Vector2 delta)
{
onDrag = true;
try
{
Screen.showCursor = fal;
}
catch
{
Screen.showCursor = fal;
}
if (controlModel == ControlModel.Rotate && !bArrive)
{
idealYaw -= delta.x * yawSensitivity * 0.02f;
idealPitch += delta.y * pitchSensitivity * 0.02f;
len = delta;
if (target) ansform.Rotate(new Vector3(delta.y, -delta.x, 0), Space.World);
}
if (controlModel == ControlModel.Translate && !bArrive)
{
if (ifDragMove)
{
if (moveTarget == null)
{
target.position = new Vector3(target.position.x + delta.x * moveSpeed, target.position.y + delta.y * moveSpeed, target.localPosition.z);// GetWorldPos( fingerPos );  }
el
{
moveTarget.position = new Vector3(moveTarget.position.x + delta.x * moveSpeed, moveTarget.position.y + delta.y * moveSpeed, moveTarget.localPosition.z);
}
}
el
{
if (moveTarget == null)
{
target.position = GetWorldPos(fingerPos);
}
el
{
moveTarget.position = GetWorldPos(fingerPos);
}
}
}
}
void FingerGestures_OnPinchMove(Vector2 fingerPos1, Vector2 fingerPos2, float delta)
{
if (controlModel == ControlModel.Zoom && !bArrive)
{
if (zoomCamera)
{
float fZoom = camera.fieldOfView - delta * pinchZoomSensitivity * 800 * Time.deltaTime;
fZoom = Mathf.Min(fZoom, maxZoom);
fZoom = Mathf.Max(fZoom, minZoom);
camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, fZoom, Time.deltaTime * smoothZoomSpeed);
// ansform.position = target.position - fZoom * ansform.forward;
}
el
{
fChangeScale = target.localScale.x + delta * pinchZoomSensitivity;
Vector3 vc = new Vector3(fChangeScale, fChangeScale, fChangeScale);
}
}
//滑动结束
void OnFingerDragEnd(int fingerIndex, Vector2 fingerPos)
{
Screen.showCursor = true;
onDrag = fal;
}
/
/把Unity屏幕坐标换算成3D坐标
Vector3 GetWorldPos(Vector2 screenPos)
{
// Camera mainCamera = Camera.main;
Camera mainCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
if (!abled)
{
mainCamera = ansform.parent.FindChild("CameraOne").GetComponent<Camera>();
}
return mainCamera.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, Mathf.Abs(target.position.z - ansform.position.z))); }
void Apply()
{
if (controlModel == ControlModel.Rotate && !bArrive)
{
yaw = Mathf.Lerp(yaw, idealYaw, Time.deltaTime * smoothOrbitSpeed);
pitch = Mathf.Lerp(pitch, idealPitch, Time.deltaTime * smoothOrbitSpeed);
}
}
bool onDrag;
Vector2 len;
void LateUpdate()
{
if (Input.GetMouButtonUp(1) || Input.GetMouButtonUp(0))
{
Screen.showCursor = true;
}
Apply();
}
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);
}
void Update()
{
///⾃由切换
if (Input.GetMouButtonDown(0))
{
controlModel = ControlModel.Translate;
}
if (Input.GetMouButtonDown(1))
{
controlModel = ControlModel.Rotate;
}
if (Input.GetAxis("Mou ScrollWheel") != 0)
{
controlModel = ControlModel.Zoom;
}
}
/// <summary>
/// 复位
/
// </summary>
public void RetValue()
{
if (moveTarget != null)
moveTarget.localPosition = moveTargetPos;
}
if (parentModel != null)
{
parentModel.localPosition = parentModelPos;
}
yaw = 0;
pitch = 0;
idealYaw = 0;
idealPitch = 0;
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

本文发布于:2023-05-11 06:24:23,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/581683.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:缩放   旋转   插件   移动
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图