当前位置:网站首页>Unity脚本的基础语法(5)-向量
Unity脚本的基础语法(5)-向量
2022-06-30 12:18:00 【ht_game】
3D游戏开发中经常需要用到向量的运算,Unity中提供了完整的向量及向量操纵方法,分别为表示二维向量的Vector2类、表示三维向量的Vector3类与表示四维向量的Vector4类。这3种向量用法基本相同。
Vector一般与postion相互联动
或者作为一些API的变量
Vector3类可以在实例化时进行赋值
public class Vector : MonoBehaviour {
public Vector3 position1 = new Vector3();
public Vector3 position2 = new Vector3(1, 2, 2);
void Start()
{
position1.x = 1;
position1.y = 2;
position1.z = 2;
}
void Update()
{
transform.position = position1;
}
}
Vector3类中常量对应的值
| 常量 | 值 |
|---|---|
| Vector3.zero | Vector(0,0,0) |
| Vector3.forward | Vector(0,0,1) |
| Vector3.right | Vector(1,0,0) |
| Vector3.down | Vector(0,-1,0) |
| Vector3.one | Vector(1,1,1) |
| Vector3.up | Vector(0,1,0) |
| Vector3.back | Vector(0,0,-1) |
| Vector3.left | Vector(-1,0,0) |
这些向量常量可以方便使用,不必一直实例化
Vector3类中有很多对向量进行操纵的方法
Vector3类中属性与方法的作用
| 属性/方法 | 作用 |
|---|---|
| magnitude | a向量的长度 |
| normalized | 向量归一化后的结果 |
| sqrMagnitude | 向量的平方长度 |
| Lerp | 两个向量之间的线性插值 |
| Slerp | 在两个向量之间进行球形插值 |
| OrthoNormalize | 使向量规范化并且彼此相互垂直 |
| MoveTowards | 从当前的位置移向目标 |
| RotateTowards | 从当前的向量移向目标 |
| SmoothDamp | 随着时间的推移,逐渐改变一个向量朝向预期的目标 |
| Scale | 两个矢量组件对应相乘 |
| Cross | 两个向量的交叉乘积 |
| Reflect | 沿着法线反射向量 |
| Dot | 两个向量的点乘积 |
| Project | 投影一个向量到另一个向量 |
| Angle | 返回两个 向量的夹角 |
| Distance | 返回两点之间的距离 |
| Min | 返回两个向量中长度较小的向量 |
| Max | 返回两个向量中长度较大的向量 |
| operator+ | 两个向量相加 |
| operator+ | 两个向量相减 |
| operator* | 两个向量相乘 |
| operator/ | 两个向量相除 |
| operator== | 两个向量是否相等 |
边栏推荐
- The realization of QT the flipping effect of QQ weather forecast window
- JMeter之性能测试流程及性能测试关注点
- MySQL composite query
- 两批次纯牛奶不合格?麦趣尔回应:正对产品大批量排查抽检
- Construction de la plate - forme universelle haisi 3559: obtenir le codage après modification du cadre de données
- SQLSERVER 查询编码是 936 简体中文GBK,那我是写936 还是写GBK?
- Substrate 源码追新导读: 波卡系波卡权重计算全面更新, Governance 2.0 版本的优化和调整
- 【MySQL】MySQL的安装与配置
- Lichuang EDA learning notes 10 common connector component identification and passive buzzer driving circuit
- After the market value evaporated by 65billion yuan, the "mask king" made steady medical treatment and focused on condoms
猜你喜欢

如何利用AI技术优化独立站客服系统?听听专家怎么说!

【OpenGL】OpenGL Examples

Hisilicon 3559 developing common sense reserves: a complete explanation of related terms

Hisilicon 3559 universal platform construction: introduction to YUV format

Hisilicon 3559 sample parsing: Venc

Instructions for legend use in SuperMap iclient3d 11i for cesium 3D scene

Analysis of the whole process of common tilt data processing in SuperMap idesktop

21. Notes on WPF binding

After the market value evaporated by 65billion yuan, the "mask king" made steady medical treatment and focused on condoms
![[qnx hypervisor 2.2 user manual]6.2.3 communication between guest and external](/img/ca/9065325ce8882d95fb24c82fb62abc.png)
[qnx hypervisor 2.2 user manual]6.2.3 communication between guest and external
随机推荐
Docker安装Mysql8和sqlyong连接报错2058的解决方法[随笔记录]
QT implementation dynamic navigation bar
Pharmacy management system
Wechat launched the picture big bang function; Apple's self-developed 5g chip may have failed; Microsoft solves the bug that causes edge to stop responding | geek headlines
Questionnaire star questionnaire packet capturing analysis
Docker installation of mysql8 and sqlyong connection error 2058 solution [jottings]
两批次纯牛奶不合格?麦趣尔回应:正对产品大批量排查抽检
Solve numpy core._ exceptions. Ufunctypeerror: UFUNC 'Add' did not contain a loop with signature matching
黑马笔记---包装类,正则表达式,Arrays类
c# 怎样能写个sql的解析器
Efficient elliptic curve point addition and multiplication in scrypt
90. (cesium chapter) cesium high level listening events
[QNX Hypervisor 2.2用户手册]6.2.3 Guest与外部之间通信
Substrate 源码追新导读: Call调用索引化, 存储层事物化全面完成
【OpenGL】OpenGL Examples
【OpenGL】OpenGL Examples
【目标跟踪】|pytracking 配置 win 编译prroi_pool.pyd
STM32 移植 RT-Thread 标准版的 FinSH 组件
Hisilicon 3559 universal platform construction: introduction to YUV format
第十三章 信号(三)- 示例演示