Search code examples
unity-game-enginevectorrotationdirection

How can I get a direction vector that's parallel to platform?


enter image description here

I would like to push obj to the edge of platform. The push power comes from the center of platform. The platform can rotate or move, so its rotation and position can be changed.

So all I need is the direction vector that's parallel to the platform regardless of the position and rotation of obj and platform.

How can I get this direction vector? Please help me..


Solution

  • Guess that the height of the moved object position from the plane, and the plane pos are known, so find some cool pseudocode. Remember that a dotProduct is a projection of one vector in the others direction, so, the height of your object to push projected in the plane's normal is:

    Vector3 objHeightProyectedInPlaneNormal = Vector3.Dot((planePos - ObjPos), yourPlaneNormal);

    Green arrow showing objHeightProyectedInPlaneNormal:

    enter image description here

    The module of that vector upwards from the plane origin, or easier the sum of that vector to the planes position should give you the push direction origin's position:

    Vector3 pushDirOrigin = planePos + objHeightProyectedInPlaneNormal

    Rounded with green pushDirOrigin:

    enter image description here

    With your object position and the push origin, you have your direction:

    Vector3 pusDir = (ObjPos - pushOrigin).normalized