服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity3D实现物体旋转缩放移动效果

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

2022-03-10 14:06qq_27361571 C#

这篇文章主要为大家详细介绍了Unity3D实现物体旋转缩放移动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Unity3D实现物体旋转缩放移动的具体代码,供大家参考,具体内容如下

由于项目运行在安卓上,运用到了插件,比较麻烦。你们可以在触发条件上进行修改,不用插件也可以。

1.下载FingerGestures 插件 下载地址 点击打开链接

2.导入插件,创建场景 将预设Finger Gestures Initializer 拖拽到 Hierarchy 视图中

3.添加脚本,拖拽到摄像机上。创建一个方块拖拽到脚本target 属性上。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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

延伸 · 阅读

精彩推荐
  • C#浅谈C# winForm 窗体闪烁的问题

    浅谈C# winForm 窗体闪烁的问题

    下面小编就为大家带来一篇浅谈C# winForm 窗体闪烁的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网7962021-12-21
  • C#C#基础之泛型

    C#基础之泛型

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。接下来通过本文给大家介绍c#基础之泛型,感兴趣的朋友一起学习吧...

    方小白7732021-12-03
  • C#C# 后台处理图片的几种方法

    C# 后台处理图片的几种方法

    本篇文章主要介绍了C# 后台处理图片的几种方法,非常具有实用价值,需要的朋友可以参考下。...

    IT小伙儿10162021-12-08
  • C#聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题,新手速来围观,一个通俗易懂的例子帮助大家更好的理解C#接口问题,感兴趣的小伙伴们可以参考一下...

    zenkey7072021-12-03
  • C#C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    这篇文章主要介绍了C#实现的文件操作封装类,结合完整实例形式分析了C#封装文件的删除,移动,复制,重命名等操作相关实现技巧,需要的朋友可以参考下...

    Rising_Sun3892021-12-28
  • C#c#学习之30分钟学会XAML

    c#学习之30分钟学会XAML

    一个界面程序的核心,无疑就是界面和后台代码,而xaml就是微软为构建应用程序界面而创建的一种描述性语言,也就是说,这东西是搞界面的...

    C#教程网8812021-12-10
  • C#C#直线的最小二乘法线性回归运算实例

    C#直线的最小二乘法线性回归运算实例

    这篇文章主要介绍了C#直线的最小二乘法线性回归运算方法,实例分析了给定一组点,用最小二乘法进行线性回归运算的实现技巧,具有一定参考借鉴价值,需要...

    北风其凉8912021-10-18
  • C#Unity3D UGUI实现缩放循环拖动卡牌展示效果

    Unity3D UGUI实现缩放循环拖动卡牌展示效果

    这篇文章主要为大家详细介绍了Unity3D UGUI实现缩放循环拖动展示卡牌效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    诗远3662022-03-11