Unity3D实现物体旋转缩放移动效果
作者:qq_27361571 发布时间:2023-07-17 22:11:58
标签: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 = false;//鼠标是否到达零件箱边界区域
//平移方式是否根据鼠标拖动距离还是直接置为鼠标位置
public bool ifDragMove = false;
//平移方式为:根据鼠标拖动距离 时,评议的速度
public float moveSpeed = 1.0f;
//是够需要画出按钮(缩放、旋转、平移)
public bool ifDrawBtn = true;
//缩放方式改为:改变相机范围
public bool zoomCamera = false;
//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 setRotation()
{
Vector3 angles = target.eulerAngles;
yaw = idealYaw = angles.y;
pitch = idealPitch = angles.x;
}
void FingerGestures_OnDragMove(Vector2 fingerPos, Vector2 delta)
{
onDrag = true;
try
{
Screen.showCursor = false;
}
catch
{
Screen.showCursor = false;
}
if (controlModel == ControlModel.Rotate && !bArrive)
{
idealYaw -= delta.x * yawSensitivity * 0.02f;
idealPitch += delta.y * pitchSensitivity * 0.02f;
len = delta;
if (target) target.transform.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 );
}
else
{
moveTarget.position = new Vector3(moveTarget.position.x + delta.x * moveSpeed, moveTarget.position.y + delta.y * moveSpeed, moveTarget.localPosition.z);
}
}
else
{
if (moveTarget == null)
{
target.position = GetWorldPos(fingerPos);
}
else
{
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);
// camera.transform.position = target.position - fZoom * camera.transform.forward;
}
else
{
fChangeScale = target.localScale.x + delta * pinchZoomSensitivity;
Vector3 vc = new Vector3(fChangeScale, fChangeScale, fChangeScale);
}
}
}
//滑动结束
void OnFingerDragEnd(int fingerIndex, Vector2 fingerPos)
{
Screen.showCursor = true;
onDrag = false;
}
//把Unity屏幕坐标换算成3D坐标
Vector3 GetWorldPos(Vector2 screenPos)
{
// Camera mainCamera = Camera.main;
Camera mainCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
if (!mainCamera.enabled)
{
mainCamera = mainCamera.transform.parent.FindChild("CameraOne").GetComponent<Camera>();
}
return mainCamera.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, Mathf.Abs(target.position.z - mainCamera.transform.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.GetMouseButtonUp(1) || Input.GetMouseButtonUp(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.GetMouseButtonDown(0))
{
controlModel = ControlModel.Translate;
}
if (Input.GetMouseButtonDown(1))
{
controlModel = ControlModel.Rotate;
}
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
controlModel = ControlModel.Zoom;
}
}
/// <summary>
/// 复位
/// </summary>
public void ResetValue()
{
if (moveTarget != null)
{
moveTarget.localPosition = moveTargetPos;
}
if (parentModel != null)
{
parentModel.localPosition = parentModelPos;
}
yaw = 0;
pitch = 0;
idealYaw = 0;
idealPitch = 0;
}
}
来源:https://blog.csdn.net/qq_27361571/article/details/51354717
0
投稿
猜你喜欢
- 如何快速判断一个元素是不是在一个集合里?这个题目是我最近面试的时候常问的一个问题,这个问题不同人都有很多不同的回答。今天想介绍一个很少有人会
- 本文实例讲述了Java * 和AOP应用。分享给大家供大家参考,具体如下:一 点睛 * 在AOP(Aspect Orient Progr
- 本文实例讲述了JAVA+Struts2获取服务器地址的方法。分享给大家供大家参考。具体实现方法如下:HttpServletRequest r
- 本文实例讲述了Java实现数据库连接池的方法。分享给大家供大家参考。具体如下:package com.kyo.connection;impo
- 1.前言在java当中,若是进行比较,大家可能第一时间想到,==或是!=,这种数学上的比较符>、接下来,我就分别介绍并演示
- 本文实例讲述了Android编程实现WebView添加进度条的方法。分享给大家供大家参考,具体如下:标准的XML界面<?xml ver
- 1. 简单说明嗨,大家好!今天给大家分享的是Mybatis-plus 插件的分页机制,说起分页机制,相信我们程序员都不陌生,今天,我就给大家
- 本文运用图片给大家介绍了C#如何检查foreach判读其是否为null,我们下面话不多说,直接来看内容吧。1、foreach遍历列表或数组时
- 将通用算法放入具体类(HeapSorter),并将通用算法必须调用的方法定义在接口(HeapSorterHandle)中,从这个接口派生出D
- 一、前言闭锁与栅栏是在多线程编程中的概念,因为在多线程中,我们不能控制线程的执行状态,所以给线程加锁,让其按照我们的想法有秩序的执行。闭锁C
- 这两天因为要做一个随机的地图生成系统,所以一直在研究随机迷宫生成算法,好吧,算是有一点小小的成果。随机迷宫生成我自己的理解简而言之分为以下几
- 一般在web应用中,对客户端提交上来的图片肯定需要进行压缩的。尤其是比较大的图片,如果不经过压缩会导致页面变的很大,打开速度比较慢,当然了如
- 懒加载 ,也称为嵌套查询 需要查询关联信息时,使用 Mybatis 懒加载特性可有效的减
- 上两片第归算法学习:1)递归算法之分而治之策略2)递归算法之归并排序上一篇学习中介绍了了递归算法在排序中的一个应用:归并排序,在排序算法中还
- 一、C#代码将html样式文件转为Word文档首先有个这样的需求,将以下网页内容下载为Word文件。html代码:<div class
- 前言近期一直在忙项目,我也是打工仔。不多说,我们开始玩一玩seata。什么都不说,我们按照惯例,先上一个图(图里不规范的使用请忽略):简单一
- 本文实例为大家分享了Unity shader实现高斯模糊效果的具体代码,供大家参考,具体内容如下正常图:高斯模糊效果图:shader代码:S
- java 算法之归并排序详解一、思想 归并排序:将一个数组排序,可以先(递归地)将它分成两半部份分别排序,然后将结果归并起来; &
- Spring Data JPA 映射VO/DTO对象在项目开发中,时常需要根据业务需求来映射VO/DTO对象(这两个概念理解感觉很模糊- 。
- 问题描述需要定时通过websocket接口来推送mysql里面最新的数据,自定义了定时器@Component@Slf4jpublic cla