1、 根据勾股定理计算两点的距离
/// <summary>/// 根据勾股定理计算两点的距离/// </summary>/// <param name="point1"></param>/// <param name="point2"></param>/// <returns></returns>private float CalculateTwoPointDistance(Vector3 point1, Vector3 point2){//根据勾股定理(a²+b²=c²)求出支撑杆长度,开c的平方根得到弦的长度float c = Vector3.Distance(point2, point1);float a = Mathf.Abs(point2.y - point1.y);return Mathf.Sqrt(Mathf.Pow(c, 2) - Mathf.Pow(a, 2));}
2、计算两条向量在某平面上的投影之间的角度。有正负值的角度。根据左手定律确定顺、逆时针旋转方向
/// <summary>/// 计算两条向量在某平面上的投影之间的角度。有正负值的角度。根据左手定律确定顺、逆时针旋转方向/// </summary>/// <param name="from"> 当前向量 </param>/// <param name="to"> 比较向量 </param>/// <returns></returns>private float SignedAngleProject(Vector3 from, Vector3 to, Vector3 plane){/* 计算投影向量 */Vector3 projectLast = Vector3.ProjectOnPlane(from, plane);Vector3 projectCur = Vector3.ProjectOnPlane(to, plane);float signed_angle = Vector3.SignedAngle(projectLast.normalized, projectCur.normalized, plane);return signed_angle;}
3、获取两点之间距离一定百分比的一个点
/// <summary>/// 获取两点之间距离一定百分比的一个点/// </summary>/// <param name="start">起始点</param>/// <param name="end">结束点</param>/// <param name="percent">起始点到目标点距离百分比</param>/// <returns></returns>public static Vector3 CalcBetweenPoint(Vector3 start, Vector3 end, float percent = 0.5f){Vector3 normal = (end - start).normalized;float distance = Vector3.Distance(start, end);return normal * (distance * percent) + start;}
4、计算目标物体相对于自身方位(前后左右上下)
/// <summary>/// 计算目标物体相对于自身方位/// </summary>/// <param name="target"></param>public float CalcTargetDirForMe(Transform target, DirectionType directionType, Transform oneself){Vector3 dir = (target.position - oneself.position).normalized; //位置差,方向float dot;if (directionType.Equals(DirectionType.Forward))dot = Vector3.Dot(target.forward, dir);//点乘判断前后 //dot >0在前 <0在后else if (directionType.Equals(DirectionType.Right))dot = Vector3.Dot(target.right, dir);//点乘判断左右else if (directionType.Equals(DirectionType.Up))dot = Vector3.Dot(target.up, dir);//点乘判断上下elsedot = 0;//中心return dot;}public enum DirectionType{Forward,Right,Up,}
5、计算目标物体相对于自身方位(前后)
/// <summary>/// 计算目标物体相对于自身方位/// </summary>/// <param name="target"></param>public float CalcTargetDirForMe(Transform target){Vector3 dir = target.position - cameraRig.position; //位置差,方向float dot = Vector3.Dot(target.forward, dir);//点乘判断前后 //dot >0在前 <0在后 return dot;}