Unity之绕轴进行旋转的操作
作者:风中追风3zZ 时间:2021-11-14 17:53:10
先上一张效果图
using UnityEngine;
using System.Collections;
public class TestRotateRound : MonoBehaviour
{
public GameObject Sphere;
private float curtTime = 0.0f;
void Update()
{
//使用C#封装好的代码RotateAround
gameObject.transform.RotateAround(Sphere.transform.position, Sphere.transform.up, 72 * Time.deltaTime);
//自己封装代码,功能和上面的相同
//RotateAround(Sphere.transform.position,Vector3.up, 72 * Time.deltaTime);
}
private void RotateAround(Vector3 center, Vector3 axis, float angle)
{
//绕axis轴旋转angle角度
Quaternion rotation = Quaternion.AngleAxis(angle, axis);
//旋转之前,以center为起点,transform.position当前物体位置为终点的向量.
Vector3 beforeVector = transform.position - center;
//四元数 * 向量(不能调换位置, 否则发生编译错误)
Vector3 afterVector = rotation * beforeVector;//旋转后的向量
//向量的终点 = 向量的起点 + 向量
transform.position = afterVector + center;
//看向Sphere,使Z轴指向Sphere
transform.LookAt(Sphere.transform.position);
}
}
补充:Unity绕x轴旋转并限制角度的陷阱
在制作FPS相机时,遇到了需要限制角度的需求,视角只能查看到-60到60度的范围,而在Unity的Transform组件中,绕x轴逆时针旋转,Transform组件的localEulerAngle会在0~360范围内递增(如图)
关键在于其中的角度转换,直接上代码
public static void RotateClampX(this Transform t, float degree, float min, float max)
{
degree = (t.localEulerAngles.x - degree);
if (degree > 180f)
{
degree -= 360f;
}
degree = Mathf.Clamp(degree, min, max);
t.localEulerAngles = t.localEulerAngles.SetX(degree);
}
补充:Unity3D 实现物体始终面向另一个物体(绕轴旋转、四元数旋转)
一开始本人纠结于在VR中,怎么利用手柄来控制物体的旋转,物体位置不变。
相当于:地球仪。更通俗点来说,就是一个棍子插到地球仪上,然后拿着棍子就可以控制地球仪转。手柄相当于那根棍子。
代码如下:
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
这句代码实现了 myTransform 始终可以根据 target 旋转,rotationSpeed控制速度。
当然上面这句话仅仅只是始终面向,还没有加上一开始记录下target的初始旋转。不然一开始就要跟着手柄转,而不是自己随意控制。对于上句的理解,我理解完便贴上。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/qq_34853692/article/details/81392838
标签:Unity,绕轴,旋转
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#使用FileStream复制一个任意文件
2022-04-17 19:55:17
![](https://img.aspxhome.com/file/2023/3/86983_0s.png)
SpringMVC中Controller类数据响应的方法
2021-10-07 21:00:08
![](https://img.aspxhome.com/file/2023/0/75710_0s.png)
在多线程中调用winform窗体控件的实现方法
2023-09-13 09:07:43
Android 仿微信发动态九宫格拖拽、删除功能
2022-08-28 12:51:03
![](https://img.aspxhome.com/file/2023/9/93279_0s.jpg)
Flutter 实现下拉刷新上拉加载的示例代码
2023-08-18 21:31:16
![](https://img.aspxhome.com/file/2023/8/88668_0s.gif)
java字符串相似度算法
2023-11-26 12:33:25
Android利用HorizontalScrollView仿ViewPager设计简单相册
2022-07-29 12:28:33
![](https://img.aspxhome.com/file/2023/5/96755_0s.png)
Java JVM程序指令码实例解析
2022-05-16 15:09:38
![](https://img.aspxhome.com/file/2023/6/81356_0s.png)
详解Java的文件与目录管理以及输入输出相关操作
2022-05-03 15:07:22
![](https://img.aspxhome.com/file/2023/0/101750_0s.jpg)
Java日期与时间类原理解析
2021-07-20 14:00:36
Unity通用泛型单例设计模式(普通型和继承自MonoBehaviour)
2023-08-24 14:53:28
synchronized及JUC显式locks 使用原理解析
2023-08-05 03:28:41
SpringMVC解析JSON请求数据问题解析
2023-06-02 21:41:32
![](https://img.aspxhome.com/file/2023/8/60578_0s.png)
一个Servlet是如何处理多个请求的?
2023-01-18 11:19:32
![](https://img.aspxhome.com/file/2023/5/70495_0s.png)
C#多线程之线程同步
2022-06-14 19:58:03
![](https://img.aspxhome.com/file/2023/2/94622_0s.jpg)
带着新人看java虚拟机01(推荐)
2023-05-28 04:38:16
![](https://img.aspxhome.com/file/2023/7/78047_0s.png)
C#基于UDP进行异步通信的方法
2022-03-20 18:23:55
Springboot+WebSocket实现一对一聊天和公告的示例代码
2022-06-16 11:32:33
SpringBoot整合canal实现数据同步的示例代码
2022-05-07 19:51:24
![](https://img.aspxhome.com/file/2023/1/61661_0s.png)
Java反射机制的简单讲解
2023-12-12 20:49:13
![](https://img.aspxhome.com/file/2023/6/64796_0s.png)