Unity--Vector3的使用

using UnityEngine;

public class Vector3Test : MonoBehaviour
{
    public Transform target;
    float smoothTime = 1;
    Vector3 velocity = Vector3.zero;

    void Start()
    {
        // 上下左右,对应坐标系上下左右
        print(Vector3.down);
        print(Vector3.up);
        print(Vector3.left);
        print(Vector3.right);

        print(Vector3.one);
        print(Vector3.zero);

        Vector3 z = Vector3.forward;// (0, 0, 1)
        Vector3 x = Vector3.right;// (1, 0, 0)
        Vector3 y = Vector3.up;// (0, 1, 0)
        Vector3 c = z - x;
        print(c.magnitude);
        print(c.sqrMagnitude);
        print(c.normalized);

        //向量是结构体,是值类型,要整体赋值
        transform.position = new Vector3(3, 3, 3);
        Vector3 pos = transform.position;
        pos.x = 10;
        transform.position = pos;

        // 两个向量夹角
        print(Vector3.Angle(z, x));

        // 两个点之间的距离
        print(Vector3.Distance(z, x));

        // 点积是一个浮点值,它等于两个矢量的大小相乘,然后乘以它们之间角度的余弦。
        print(Vector3.Dot(z, x));

        // 从a, b, c三个点获得三角形的法线
        print(GetNormal(z, x, y));

        Vector3 vector = new Vector3(1, 1, 1);
        // 向量vector在z向量方向上的投影
        print(Vector3.Project(vector, z));

        // a,b向量之间插值
        print(Vector3.Lerp(z, x, 0.5f));
        print(Vector3.LerpUnclamped(z, x, 0.5f));

        // 返回最大长度的向量
        print(Vector3.Max(z, x));
        print(Vector3.Min(z, x));

        // 每个分量的乘积
        // 结果(2, 6, 12)=(1 * 2 + 2 * 3 + 3 * 4)
        print(Vector3.Scale(new Vector3(1, 2, 3), new Vector3(2, 3, 4)));

        // 求反射向量 Vector3 Reflect(Vector3 inDirection, Vector3 inNormal);
        // inDirection入射向量, inNormal垂直平面的法线
        print(Vector3.Reflect(new Vector3(-1, -1, -1), Vector3.up));

        // 两个向量间的球面插值
        Vector3.Slerp(Vector3.forward, Vector3.right, 0.5f);

    }

    // 从a, b, c三个点获得三角形的法线
    Vector3 GetNormal(Vector3 a, Vector3 b, Vector3 c)
    {
        // 找到对应三角形的两个边
        Vector3 side1 = b - a;
        Vector3 side2 = c - a;

        // 求得叉乘结果,并归一化
        // Vector3.Cross叉乘结果,同时垂直side1和side2的向量
        return Vector3.Cross(side1, side2).normalized;
    }

    private void Update()
    {
        // 随着时间的推移逐渐将向量更改为期望的目标。
        //transform.position = Vector3.SmoothDamp(transform.position, target.position, ref velocity, smoothTime);
    }
}

 

你可能感兴趣的:(Unity,Vector3)