Search Unity

Getting point between two vectors help math experts!

Discussion in 'Scripting' started by Dolzen, Sep 1, 2015.

  1. Dolzen

    Dolzen

    Joined:
    Mar 26, 2014
    Posts:
    90
    In a Vector3 world, I have point A and point B, and all I want to do is get point C as you can see in this image, I know it's possible but may require advanced math techniques that I don't have, The only reason I want to do this is because I want to rotate a gameobject in pont A in the direction of point B but only in his local Y axis, thanks guys!



    This is the code I'm using to rotate game object in pont A in the direction of point B:

    pointA.rotation = Quaternion.Slerp(
    pointA.rotation,
    Quaternion.LookRotation((pointB - pointA).normalized),
    Time.deltaTime
    );
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    georetro likes this.
  3. georetro

    georetro

    Joined:
    Jan 18, 2013
    Posts:
    218
    @JamesLeeNZ Yup if you want to get point C in world space like the diagram states, you should use Cross like James has listed.
     
  4. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Maybe the ClosestPointsOnTwoLines method found here can help.
    http://wiki.unity3d.com/index.php/3d_Math_functions

    Ill paste it here in case the link dies
    Code (CSharp):
    1.     public static bool ClosestPointsOnTwoLines(out Vector3 closestPointLine1, out Vector3 closestPointLine2, Vector3 linePoint1, Vector3 lineVec1, Vector3 linePoint2, Vector3 lineVec2){
    2.         closestPointLine1 = Vector3.zero;
    3.         closestPointLine2 = Vector3.zero;
    4.         float a = Vector3.Dot(lineVec1, lineVec1);
    5.         float b = Vector3.Dot(lineVec1, lineVec2);
    6.         float e = Vector3.Dot(lineVec2, lineVec2);
    7.         float d = a*e - b*b;
    8.         //lines are not parallel
    9.         if(d != 0.0f){
    10.             Vector3 r = linePoint1 - linePoint2;
    11.             float c = Vector3.Dot(lineVec1, r);
    12.             float f = Vector3.Dot(lineVec2, r);
    13.             float s = (b*f - c*e) / d;
    14.             float t = (a*f - c*b) / d;
    15.             closestPointLine1 = linePoint1 + lineVec1 * s;
    16.             closestPointLine2 = linePoint2 + lineVec2 * t;
    17.             return true;
    18.         }
    19.         else{
    20.             return false;
    21.         }
    22.     }    

    You may want to edit that code to instead of returning bool and using out, to just return what you want.
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The cross product returns a vector that is perpendicular to both the vectors passed in. Not sure this will help you.

    I'm struggling to see how A, B and C are related to each other...
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401