向量包含两个东西: 距离 和方向.
aPos = transform.position;
float lengh = aPos.magnitude;// 长度 ,从Vector3.zero到aPos的距离
Vector3 dir = aPos.normalized;// 方向
叉乘 的结果 是向上还是向下 ,在不同位置时结果不同
//判断两个向量之间的左右位置关系
Vector3 C = Vector3.Cross(B.position, A.position);
if (C.y > 0)
{
print("A在B的右侧");
}
else
{
print("A在B的左侧");
}
aPos = a.position;
bPos = b.position;
var c = Vector3.Cross(bPos, aPos);
Debug.DrawLine(Vector3.zero, c, Color.black, 0.1f);
Debug.DrawLine(Vector3.zero, a.position, Color.black, 0.2f);
Debug.DrawLine(Vector3.zero, b.position, Color.black, 0.2f);
Quaternion.LookRotation(Vector3 n)
void Update()
{
aPos = a.position;
bPos = b.position;
var c = Vector3.Cross(bPos,aPos);
float dot = Vector3.Dot(aPos.normalized, bPos.normalized);
angle = Mathf.Acos(dot) * Mathf.Rad2Deg;
if (c.y < 0)
{
angle = 360 - angle;
tmp.text = "A is B to left";
}
else
{
tmp.text = "A is B to right";
}
Debug.DrawLine(Vector3.zero, a.position, Color.black, 0.2f);
Debug.DrawLine(Vector3.zero, b.position, Color.black, 0.2f);
}
角度累加 按照顺时针相加 ,没有360度最高359.9
已原点为中心点进行旋转
var a = aT.position;
//绕y轴旋转90度
var r = Quaternion.AngleAxis(90, new Vector3(0, 1, 0));
//点c = 坐标1 + 方向 * 距离
var c = Vector3.zero + r * (a - Vector3.zero).normalized * 5f;
Debug.DrawLine(Vector3.zero, aT.position, Color.black, 0.2f);
Debug.DrawLine(Vector3.zero, c, Color.black, 0.2f);
C_transform.position = c;
已知有点A 和点B,代码: Vector3 A,B;
求红色箭头方向算法,已知红色箭头垂直于蓝色箭头方向的
已A点为中心点进行旋转
a = aT.position;
b = bT.position;
//绕y轴旋转90度
var r = Quaternion.AngleAxis(90, new Vector3(0, 1, 0));
//点c = 坐标1 + 方向 * 距离
var c = a + r * (b - a).normalized * 5f;
Debug.DrawLine(b, aT.position, Color.black, 0.2f);
Debug.DrawLine(a, c, Color.black, 0.2f);
C_transform.position = c;