Unity常用旋转API

简介: Unity中的旋转通常可以用Transform 直接控制和 rotation 控制两种方式。一、Transform控制(工程中的scene1)1.1 Transform.Rotate旋转某个角度函数定义[csharp] view plain copypublic void Rotate...

Unity中的旋转通常可以用Transform 直接控制和 rotation 控制两种方式。

一、Transform控制(工程中的scene1)

1.1 Transform.Rotate

旋转某个角度

函数定义

[csharp] view plain copy

public void Rotate(Vector3 eulerAngles);
public void Rotate(Vector3 axis, float angle);
public void Rotate(Vector3 eulerAngles, Space relativeTo);
public void Rotate(float xAngle, float yAngle, float zAngle);
public void Rotate(Vector3 axis, float angle, Space relativeTo);
public void Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo);
这个函数通常用于Update函数中,用于不断的旋转。如下

[csharp] view plain copy

void Update () {
//以每秒rotateSpeed的速度绕着y轴旋转
transform.Rotate(0, rotateSpeed * Time.deltaTime, 0);
}
1.2 Transform.RotateAround

以某点为中心旋转。

函数定义

[csharp] view plain copy

public void RotateAround(Vector3 axis, float angle);
public void RotateAround(Vector3 point, Vector3 axis, float angle);
这个函数通常也在Update中,不断地围绕着点运动。如下:

[csharp] view plain copy

void Update ()
{
//在Y轴以每秒rotateSpeed的速度围绕着transformCenter 旋转。
//这会改变物体的位置和旋转。
transform.RotateAround(transformCenter.position,Vector3.up,rotateSpeed * Time.deltaTime);
}
1.3 Transform.eulerAngles

设置物体的欧拉角为某个度数

属性定义

[csharp] view plain copy

public Vector3 eulerAngles { get; set; }
直接设置即可,一般不用于Update函数中递增。如下

[csharp] view plain copy

void Update ()
{
//设置旋转角度为一个固定值
//旋转作为欧拉角度。
//x、y、z角代表绕z轴旋转z度,绕x轴旋转x度,绕y轴旋转y度(这个顺序)。
//仅使用者这个变量读取和设置角度到绝对值。不要递增它们,当超过角度360度,它将失败。使用Transform.Rotate替代。
transform.eulerAngles = new Vector3(0,0,60);
}

1.4 Transform.LookAt

旋转物体,使其朝向某个位置

函数定义

[csharp] view plain copy

public void LookAt(Transform target);
public void LookAt(Vector3 worldPosition);
public void LookAt(Transform target, Vector3 worldUp);
public void LookAt(Vector3 worldPosition, Vector3 worldUp);
简单例子:

[csharp] view plain copy

public class SimpleRotate4 : MonoBehaviour
{

public float rotateSpeed = 20;
public float moveSpeed = 4;
private float curY;
private bool moveDown = false;
public Transform transformCenter;
void Update ()
{
//这段代码只是让物体不断的上下运动
moveDown = moveDown ? curY > -4 : curY > 4;
curY -= moveDown ? Time.deltaTime moveSpeed : -Time.deltaTime moveSpeed;
transform.position = new Vector3(-2, curY, 0);

//朝向目标
//当该物体设置了LookAt并指定了目标物体时,该物体的z轴将始终指向目标物体
transform.LookAt(transformCenter);
}
}

1.5 Transform.forward

强制改变z轴朝向。

属性定义

[csharp] view plain copy

public Vector3 forward { get; set; }
例子:

[csharp] view plain copy

public class SimpleRotate5 : MonoBehaviour
{
public float rotateSpeed = 20;
public float moveSpeed = 4;
private float curY;
private bool moveDown = true;
public Transform transformCenter;

void Update ()
{

//这段代码只是让物体不断的上下运动
moveDown = moveDown ? curY > -4 : curY > 4;
curY -= moveDown ? Time.deltaTime moveSpeed : -Time.deltaTime moveSpeed;
transform.position = new Vector3(2, curY, 0);

//强制设置z轴朝向
//本例子和LookAt区别不大
transform.forward = transformCenter.position - transform.position;

}
}

二、Rotation 与 Quaternion(对应scene2)

2.1 Quaternion.eulerAngles

四元数的欧拉角

属性定义

[csharp] view plain copy

public Vector3 eulerAngles { get; set; }
实例

[csharp] view plain copy

void Update ()
{
//功能类似于 transform.eulerAngles = new Vector3(0,0,60);
//但是不能直接设置rotation.eulerAngles 需要一个完整的Quaternion
Quaternion q1 = Quaternion.identity;
q1.eulerAngles = new Vector3(0,60,0);
transform.rotation = q1;
}
2.2 Quaternion.SetFromToRotation (建议参考scene2场景来学习)

创建一个从fromDirection到toDirection的旋转

函数定义

[csharp] view plain copy

public void SetFromToRotation(Vector3 fromDirection, Vector3 toDirection);
实例:

[csharp] view plain copy

public class QuaternionRotate2 : MonoBehaviour
{
public Transform A;
public Transform B;
public Transform C;
public float moveSpeed = 4;
private float curY;
private bool moveDown = false;
private Quaternion q1 = Quaternion.identity;
void Update ()
{
moveDown = moveDown ? curY > -4 : curY > 4;
curY -= moveDown ? Time.deltaTime moveSpeed : -Time.deltaTime moveSpeed;
B.position = new Vector3(4, curY, 0);

//创建一个从fromDirection到toDirection的旋转。
q1.SetFromToRotation(A.position,B.position);
C.rotation = q1;

Debug.DrawLine(Vector3.zero, A.position,Color.red);
Debug.DrawLine(Vector3.zero, B.position, Color.green);
Debug.DrawLine(C.position, C.position + new Vector3(0,2,0), Color.black);
Debug.DrawLine(C.position, C.TransformPoint(0,2,0), Color.blue);
}
}
2.3 Quaternion.SetLookRotation

旋转物体到某个角度

函数定义

[csharp] view plain copy

public void SetLookRotation(Vector3 view);
public void SetLookRotation(Vector3 view, Vector3 up);
实例:

[csharp] view plain copy

public class QuaternionRotate3 : MonoBehaviour
{
private Quaternion q1 = Quaternion.identity;
public Transform transformCenter;
void Update ()
{
//相当于transform.LookAt
q1.SetLookRotation(transformCenter.position);
transform.rotation = q1;
}
}

2.4 Quaternion.AngleAxis

函数定义

[csharp] view plain copy

public static Quaternion AngleAxis(float angle, Vector3 axis);
围绕axis轴,旋转angle度。

实例:

[csharp] view plain copy

void Update ()
{
//绕y轴旋转60度
transform.rotation = Quaternion.AngleAxis(60,Vector3.up);
}
2.5 Quaternion.Slerp

球形插值。

函数定义

[csharp] view plain copy

public static Quaternion Slerp(Quaternion a, Quaternion b, float t);
可以用于旋转值的不断靠近。如下:

[csharp] view plain copy

public class QuaternionRotate5 : MonoBehaviour
{
public Transform A;
public float rotateSpeed = 10;
void Update () {
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(A.position - transform.position), 0.8f);
}
}

更多unity2018的功能介绍请到paws3d爪爪学院查找。

相关文章
|
10月前
|
机器学习/深度学习 编解码 数据可视化
【unity本站最全系列】unity常用API大全一篇文章足以(万字详解)不信你不收藏
【unity本站最全系列】unity常用API大全一篇文章足以(万字详解)不信你不收藏
699 1
|
4月前
|
API C# 图形学
【Unity 3D】常见API的讲解以及在C#脚本中的执行(附源码)
【Unity 3D】常见API的讲解以及在C#脚本中的执行(附源码)
64 1
|
10月前
【Unity3D--自由观察模型】模型自动旋转+触屏旋转和缩放
展示3D模型,同时实现模型自动旋转和触屏旋转和缩放
225 0
|
10月前
|
API 图形学
【2023unity游戏制作-mango的冒险】-前六章API,细节,BUG总结小结
【2023unity游戏制作-mango的冒险】-前六章API,细节,BUG总结小结
95 0
|
10月前
|
存储 API 图形学
【2023unity游戏制作-mango的冒险】-2.开始画面API制作
【2023unity游戏制作-mango的冒险】-2.开始画面API制作
104 0
|
10月前
|
编译器 API 图形学
unity常用API基础知识
常用API基础知识
151 0
|
10月前
|
图形学
unity基础-坦克炮管旋转发射炮弹(向量基础,射线碰撞,物体实例化)
unity基础-坦克炮管旋转发射炮弹(向量基础,射线碰撞,物体实例化)
248 0
|
图形学
Unity 之 获取物体的旋转角正确数值
不管父物体如何设置,都能获取到物体本身旋转角度的正确数值
929 0
|
图形学
Unity3D旋转
Unity3D旋转
98 0
|
JSON API 图形学
Unity 接入高德开放API - 天气查询
Unity 接入高德开放API - 天气查询
413 1
Unity 接入高德开放API - 天气查询