点投影到平面
1
public Transform targetPlane;
public Transform projectPoint;
public Vector3 normal = Vector3.up;
void Update()
{
Debug.Log(normal);
var Worldpos = transform.position;
var Localpos = targetPlane.InverseTransformPoint(Worldpos);
var dis = Vector3.Dot(Localpos, normal);
var vecN = normal * dis;
var planepos = Localpos - vecN;
projectPoint.position = targetPlane.TransformPoint(planepos);
}
2
public Transform targetPlane;
public Transform projectPoint;
void Update()
{
var worldpos = transform.position;
var targetPlanePos = targetPlane.position;
var normal = targetPlane.transform.up;
var pos = PointToPlane(worldpos, targetPlanePos, normal);
projectPoint.position = pos;
}
Vector3 PointToPlane(Vector3 worldpos, Vector3 targetPlanePos, Vector3 planeNormal)
{
var localpos = worldpos - targetPlanePos;
var dis = Vector3.Dot(localpos, planeNormal);
var vecN = planeNormal * dis;
return worldpos -vecN;
}