Search code examples
c#unity-game-engineplane

A plane that goes through two points and is parallel to player.transform.forward


I've got a player with a sword and the two points on a target are from OnTriggerEnter and OnTriggerExit. Then I want to create a slicing plane that is parallel to player.transform.forward - it can be vertical, horizontal or on an angle (parallel to the player forward - which is always horizontal).

To define the plane I need its normal/up vector and one of the two points and it is infinite. So I'd like code for the plane's normal based on position1, position2, and player.transform.forward.


Solution

  • Based on LoïcLeGrosFrère I did this: (EzySlice also has a Plane class)

    UnityEngine.Plane plane = new UnityEngine.Plane(sliceStartPos, sliceEndPos, playerPos);
    
    SlicedHull hull = objectToSlice.Slice(sliceStartPos, plane.normal, crossMat);
    

    An alternative:

    public Vector3 GetPlaneNormal(Vector3 a, Vector3 b, Vector3 c)
    {
        return Vector3.Normalize(Vector3.Cross(b - a, c - a));
    }
    Vector3 planeNormal = GetPlaneNormal(sliceStartPos, sliceEndPos, playerPos);
    
    SlicedHull hull = objectToSlice.Slice(sliceStartPos, planeNormal, crossMat);