LoginSignup
0
0

More than 3 years have passed since last update.

PlaneとRayの交差する点を求めるスクリプト

Posted at

マウスポインターをコライダー内部に閉じ込める方法 内に書いたスクリプトですが、記事を分けておかないと探せないので別にしました。

CrossPointOnPlane
    //Planeで取得
    public static Vector3 CrossPointOnPlane(Plane plane, Ray ray)
    {
        float enter = 0.0f;
        plane.Raycast(ray, out enter);

        return ray.GetPoint(enter);
    }
    //平面上の点PlanePointと法線PlaneNormalで取得
    public static Vector3 CrossPointOnPlane(Vector3 PlaneNormal, Vector3 PlanePoint, Ray ray, float distance = 1000.0f)
    {
        Vector3 result = Vector3.zero;

        Vector3 v1 = ray.origin - PlanePoint;
        Vector3 v2 = ray.origin + (ray.direction * distance) - PlanePoint;

        float d1 = Mathf.Abs(Vector3.Dot(v1, PlaneNormal));
        float d2 = Mathf.Abs(Vector3.Dot(v2, PlaneNormal));

        float denom = d1 + d2;

        if (denom > 0)
        {
            result = ray.GetPoint(distance / denom * d1);
        }
        return result;
    }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0