Comments and answers for "Vector3.Project, how to?"
http://answers.unity.com/questions/598060/vector3project-how-to.html
The latest comments and answers for the question "Vector3.Project, how to?"Answer by Bunny83
http://answers.unity.com/answers/598112/view.html
It's pretty easy. You create a vector(V1) from your rope transform (blue dot) to your player (red dot). As normal vector you use the rope-transform-forward vector (i guess your line is along the forward vector). Since .forward is already normalized you don't have to normalize it.
then just insert V1 and as normal .forward into Vector3.Project and you get the vector(V2) from the ropeT (blue) to your desired point P (yellow).
Finally you just have to add the rope transform position and the projected vector V2 together and you get the world position of the point P.
<img src="https://dl.dropboxusercontent.com/u/7761356/UnityAnswers/Images/Vector3Project.PNG">
An example:
//C#
public Transform rope;
public Transform player;
Vector3 ProjectPointOnRope(Vector3 aPoint)
{
Vector3 V1 = aPoint - rope.position;
Vector3 V2 = Vector3.Project(V1, rope.forward);
return rope.position + V2;
}
void Test()
{
Vector3 projectedPlayer = ProjectPointOnRope(player.position);
// ...
}
In one line it would be:
// C# and UnityScript:
var projectedPlayer = rope.position + Vector3.Project(player.position - rope.position, rope.forward);
***edit***
The above method could be generalized as extension method for the Ray struct:
public static Vector3 Project(this Ray aRay, Vector3 aPoint)
{
Vector3 V1 = aPoint - aRay.origin
Vector3 V2 = Vector3.Project(V1, aRay.direction);
return aRay.origin + V2;
}
This would allow you to project any point onto the given ray:
var ray = new Ray(rope.position, rope.forward);
var projectedPlayer = ray.Project(player.position);Mon, 16 Dec 2013 17:43:16 GMTBunny83Answer by robertbu
http://answers.unity.com/answers/598089/view.html
You can get the position on the rope by intersecting the line that is the rope with a plane facing up at the correct 'y' height. Take a look at the Wiki Math3D functions:
[http://wiki.unity3d.com/index.php/3d_Math_functions][1]
In particular look at LinePlaneIntersection(). The plane normal with be Vector3.up, the plane point with be Vector3(0,y,0), the line point will be rope.position, and the line vector will be rope.up.
Note this will only work if you are guaranteed that rope exist at the specified 'y' height. If there is any chance that the player will be below or above the rope, then you can use Unity's mathematical Plane class and raycast from each end of the rope to generate the position and figure out if the rope exists at that specific height. Or you can just check the 'y' height of each end of the rope after doing the line/plane intersection calculation.
There are computationally more efficient ways to do this calculation, but unless you are going to be doing it many times a frame, probably not worth the effort to code them.
[1]: http://wiki.unity3d.com/index.php/3d_Math_functionsMon, 16 Dec 2013 17:17:05 GMTrobertbuComment by ThiagoTejo
http://answers.unity.com/comments/598081/view.html
http://answers.unity3d.com/questions/598010/whats-the-best-way-to-get-the-distance-between-two.html
Someone awsered there! :)
I was looking for the X distance between two Vector3 in their current rotations, something like that.
Thanks for your help too!Mon, 16 Dec 2013 17:01:45 GMTThiagoTejoComment by flaviusxvii
http://answers.unity.com/comments/598076/view.html
I was trying to clarify.. are you looking for the point on the rope that is closest to the current player position?Mon, 16 Dec 2013 16:54:30 GMTflaviusxviiComment by ThiagoTejo
http://answers.unity.com/comments/598074/view.html
Hmm, so how can i Achieve this, then?
But I guess its better finding the projection of the player on a specific axis, because there's ton of other circunstances in wich i need something like this...Mon, 16 Dec 2013 16:51:28 GMTThiagoTejoComment by flaviusxvii
http://answers.unity.com/comments/598069/view.html
I may have misunderstood your prior question. Do you just want the closest point on the rope to the player. Because that's a little different than what I suggested.Mon, 16 Dec 2013 16:47:53 GMTflaviusxvii