在Unity中,Vector类是一系列用于表示和操作向量的结构体,是游戏开发中不可或缺的数学工具。Vector类包括Vector2(二维向量)、Vector3(三维向量)和Vector4(四维向量),它们用于处理位置、方向、速度、力等概念。本文将详细介绍Vector类的各个类型、属性、方法、使用场景,并提供丰富的示例代码。
Vector 类概述
Vector类是Unity数学库的核心部分,用于表示多维空间中的点或方向。它们是值类型(struct),具有高效的性能。Vector类支持向量运算,如加法、减法、点积、叉积等。
- Vector2:表示二维向量,常用于2D游戏、UI坐标、纹理坐标等。
- Vector3:表示三维向量,最常用,用于3D空间中的位置、方向、速度等。
- Vector4:表示四维向量,常用于齐次坐标、颜色(RGBA)、矩阵运算等。
Vector2 类详解
Vector2用于二维空间,包含x和y两个分量。
Vector2 的属性
x, y:向量的分量。
magnitude:向量长度(模)。
normalized:单位向量。
sqrMagnitude:向量长度的平方(计算更快)。
Vector2 的常用方法
Vector2.Distance(a, b):两点距离。
Vector2.Angle(a, b):两向量夹角。
Vector2.Dot(a, b):点积。
Normalize():归一化向量。
Vector2 的使用场景
Vector2主要用于2D游戏开发、UI布局、纹理映射等。
示例:计算两点距离
1 2 3 4 5 6 7 8 9 10 11 12 13
| using UnityEngine;
public class Vector2Example : MonoBehaviour { public Vector2 pointA = new Vector2(0, 0); public Vector2 pointB = new Vector2(3, 4);
void Start() { float distance = Vector2.Distance(pointA, pointB); Debug.Log("Distance: " + distance); } }
|
示例:向量归一化
1 2 3 4 5 6 7 8 9 10 11 12
| using UnityEngine;
public class Vector2Normalize : MonoBehaviour { void Start() { Vector2 vec = new Vector2(3, 4); Vector2 normalized = vec.normalized; Debug.Log("Original: " + vec + ", Normalized: " + normalized); } }
|
示例:2D移动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| using UnityEngine;
public class Move2D : MonoBehaviour { public float speed = 5f; private Vector2 position;
void Start() { position = transform.position; }
void Update() { Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); position += input * speed * Time.deltaTime; } }
|
Vector3 类详解
Vector3是Unity中最常用的向量类,用于三维空间。
Vector3 的属性
x, y, z:向量的分量。
magnitude, normalized, sqrMagnitude:同Vector2。
- 静态属性:
Vector3.zero(零向量)、Vector3.one(全1向量)、Vector3.up(上方向)、Vector3.right(右方向)、Vector3.forward(前方向)。
Vector3 的常用方法
Vector3.Distance(a, b):距离。
Vector3.Angle(a, b):夹角。
Vector3.Dot(a, b):点积。
Vector3.Cross(a, b):叉积(结果是垂直于两向量的向量)。
Vector3.Lerp(a, b, t):线性插值。
Vector3.MoveTowards(current, target, maxDistance):朝目标移动。
Vector3 的使用场景
Vector3广泛用于3D游戏的位置、旋转、物理模拟等。
示例:对象移动
1 2 3 4 5 6 7 8 9 10 11 12
| using UnityEngine;
public class MoveObject : MonoBehaviour { public float speed = 5f;
void Update() { transform.position += Vector3.right * speed * Time.deltaTime; } }
|
示例:计算方向和距离
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| using UnityEngine;
public class DirectionExample : MonoBehaviour { public Transform target;
void Update() { if (target != null) { Vector3 direction = target.position - transform.position; float distance = direction.magnitude; Vector3 normalizedDirection = direction.normalized; Debug.Log("Distance: " + distance + ", Direction: " + normalizedDirection); } } }
|
示例:使用叉积计算垂直向量
1 2 3 4 5 6 7 8 9 10 11 12
| using UnityEngine;
public class CrossProductExample : MonoBehaviour { void Start() { Vector3 a = Vector3.up; Vector3 b = Vector3.right; Vector3 cross = Vector3.Cross(a, b); Debug.Log("Cross product: " + cross); } }
|
示例:线性插值移动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| using UnityEngine;
public class LerpExample : MonoBehaviour { public Vector3 startPosition = Vector3.zero; public Vector3 endPosition = new Vector3(10, 0, 0); public float duration = 2f; private float timeElapsed = 0f;
void Update() { timeElapsed += Time.deltaTime; float t = Mathf.Clamp01(timeElapsed / duration); transform.position = Vector3.Lerp(startPosition, endPosition, t); } }
|
示例:物理速度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| using UnityEngine;
public class PhysicsVelocity : MonoBehaviour { private Rigidbody rb;
void Start() { rb = GetComponent<Rigidbody>(); }
void FixedUpdate() { Vector3 force = new Vector3(0, 10, 0); rb.AddForce(force); Debug.Log("Velocity: " + rb.velocity); } }
|
Vector4 类详解
Vector4用于四维空间,常用于齐次坐标、颜色表示等。
Vector4 的属性
x, y, z, w:四个分量。
magnitude, normalized, sqrMagnitude:同上。
Vector4 的常用方法
- 基本运算同Vector2/Vector3。
- 常用于矩阵变换。
Vector4 的使用场景
Vector4用于高级数学运算,如透视投影、颜色(RGBA)、四元数扩展等。
示例:颜色表示
1 2 3 4 5 6 7 8 9 10 11
| using UnityEngine;
public class ColorVector4 : MonoBehaviour { void Start() { Vector4 color = new Vector4(1f, 0.5f, 0f, 1f); Debug.Log("Color: " + color); } }
|
示例:齐次坐标
1 2 3 4 5 6 7 8 9 10 11 12
| using UnityEngine;
public class HomogeneousCoordinates : MonoBehaviour { void Start() { Vector3 point3D = new Vector3(1, 2, 3); Vector4 homogeneous = new Vector4(point3D.x, point3D.y, point3D.z, 1f); Debug.Log("Homogeneous: " + homogeneous); } }
|
向量运算详解
基本运算
- 加法/减法:
Vector3 result = a + b;
- 标量乘法:
Vector3 result = vec * 2f;
- 点积:
float dot = Vector3.Dot(a, b); // 用于判断方向相似性
- 叉积:
Vector3 cross = Vector3.Cross(a, b); // 用于计算垂直向量
示例:向量运算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| using UnityEngine;
public class VectorOperations : MonoBehaviour { void Start() { Vector3 a = new Vector3(1, 2, 3); Vector3 b = new Vector3(4, 5, 6); Vector3 sum = a + b; Vector3 scaled = a * 2; float dot = Vector3.Dot(a, b); Vector3 cross = Vector3.Cross(a, b); Debug.Log("Sum: " + sum + ", Scaled: " + scaled + ", Dot: " + dot + ", Cross: " + cross); } }
|
性能优化建议
- Vector是值类型,传递时会复制;对于频繁使用,考虑使用ref或缓存。
- 使用sqrMagnitude代替magnitude进行比较(避免开方运算)。
- 在Update中避免创建新向量;使用静态方法或预计算。
通过掌握Vector类,你可以高效处理Unity中的空间计算、物理模拟和动画效果,为游戏开发提供坚实的数学基础。