C# Vector3

Vector3 三维向量

表示3D的向量和点

Vector3.Angle 角度

由from和to两向量返回一个角度。形象的说,from和to的连线和它们一个指定轴向的夹角

public Transform target;
void Update() {
    Vector3 targetDir = target.position - transform.position;
    Vector3 forward = transform.forward;
    float angle = Vector3.Angle(targetDir, forward);
    if (angle < 5.0F)
        print("close");
}

Vector3.back 向后

写Vector3(0, 0, -1)的简码,也就是向z轴负方向

void Example() {
    transform.position += Vector3.back * Time.deltaTime;
}

Vector3.ClampMagnitude 限制长度

返回原向量的拷贝,并且它的模最大不超过maxLength所指示的长度。

也就是说,限制向量长度到一个特定的长度。

public Vector3 centerPt;
public float radius;
void Update() {
    Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    Vector3 newPos = transform.position + movement;
    Vector3 offset = newPos - centerPt;
    transform.position = centerPt + Vector3.ClampMagnitude(offset, radius);
}

Vector3.Cross 叉乘

两个向量的交叉乘积

Vector3 GetNormal(Vector3 a, Vector3 b, Vector3 c) {
    Vector3 side1 = b - a;
    Vector3 side2 = c - a;
    return Vector3.Cross(side1, side2).normalized;
}

Vector3.Distance 距离

返回a和b之间的距离

public Transform other;
void Example() {
    if (other) {
        float dist = Vector3.Distance(other.position, transform.position);
        print("Distance to other: " + dist);
    }
}

Vector3.Dot 点乘

两个向量的点乘积

public Transform other;
void Update() {
    if (other) {
        Vector3 forward = transform.Tran
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值