1. Angle :计算两个向量的夹角
Vector3.Angle(trans1.position,trans2.position);
2. SqrMagnitude: 向量的模长平方,比较向量的长度的大小时比magnitude更省性能,因为少个内部开方。
3. ClampMagnitude: 返回向量,长度不会大于设定的length,如果原向量模长小于length,返回向量不变化,但是如果模长大于length,返回向量模长限制为length。
Vector3.ClampMagnitude(target,length);
4. OrthoNormalize:使向量规范化,并且返回该向量在三维空间的两个垂直向量
Vector3.OrthoNormalize( ref normal, ref tangent, ref binormal);
ref是代表把向量标准化。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestVector : MonoBehaviour
{
public Transform t1;
private Vector3 tangent;
private Vector3 binNormal;
private void Update()
{
Vector3 norm = t1.position;
//计算norm的两个垂直向量
Vector3.OrthoNormalize(ref norm, ref tangent, ref binNormal);
Debug.DrawLine(Vector3.zero, norm);
Debug.DrawLine(Vector3.zero, tangent, Color.red);
Debug.DrawLine(Vector3.zero, binNormal, Color.green);
}
}
运行结果如下:
5
. Vector3.ProjectOnPlane 向量投影:投影向量到一个平面上由垂直到该平面的法线定义
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestVector : MonoBehaviour
{
public Transform t1;
private void Update()
{
Vector3 norm = t1.position;
//计算t1物体在地面上的投影project
Vector3 project = Vector3.ProjectOnPlane(norm, Vector3.up);
Debug.DrawLine(Vector3.zero, norm);
Debug.DrawLine(Vector3.zero, project, Color.red);//投影线
}
}
6
. Vector3.Reflect 沿着法线反射向量,类似于物理的入射光、反射光
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestVector : MonoBehaviour
{
public Transform t1;
private void Update()
{
Vector3 norm = t1.position;
//计算norm的反射向量reflect
Vector3 reflect = Vector3.Reflect(norm, new Vector3(0,1,0));
Debug.DrawLine(Vector3.zero, norm);
Debug.DrawLine(Vector3.zero, new Vector3(0,1, 0), Color.yellow);
Debug.DrawLine(Vector3.zero, reflect, Color.red);//反射向量
}
}
版权声明:本文为qq_40229737原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。