Comments and answers for "Project a vector onto a plane."
http://answers.unity.com/questions/1456941/project-a-vector-onto-a-plane.html
The latest comments and answers for the question "Project a vector onto a plane."Comment by MariusRu on MariusRu's answer
http://answers.unity.com/comments/1540792/view.html
Thanks Bunny83 for giving me your best guess answer (in spite of initially stating my question did not make much sense). It made me realize what exactly it is that I find hard to mentally visualize: "we further rotate our (0, 0.707, 0.707).vector around y (0,1,0) also be 45° This will lead to (0.5, 0.707, 0.5)". So I guess this is not a Unity-specific problem at all but I need to instead wrap my head around some geometry textbook.Fri, 10 Aug 2018 10:32:06 GMTMariusRuAnswer by Bunny83
http://answers.unity.com/answers/1540790/view.html
If you have a question you should ask a question and not posting an answer.
<br>
Sorry but your question doesn't really make much sense. What do you mean by " 45° relative to the plane"? Note that "rotated vector" could mean anything. What is your starting vector and around which axis do you actually rotate? Do you apply euler angles rotation? What is even your plane normal? You can't get (0, 0.71, 0) when you have a normal that points in the world up direction (0,1,0). That means you clearly use a different plane / plane normal.
<br>
Whatever you tried to ask here doesn't seem to be related to the **question that has been asked here** since you have different vectors. Please don''t try to hijack the question of someone else. Answers which are posted to a question have to answer the question that was asked here. If you have a similar question but this one doesn't fully answer your specific case, ask a seperate question.
<br>
<h2> However</h2>
If you do ask a question make sure you include all necessary information to understand your case. If you have a vector tell us it's initial state. If you apply rotations to it, tell us around which axis.
<br>
Anyways i try to give a best guess answer here. However if it doesn't clear up your confusion, please ask a seperate question.:
<br>
Since in your second example the z component of your vector is 0 we can assume your plane normal is most likely either forward or -forward (0,0,1). So after a projection only the x and y components remains. Lets assume further that your initial vector you're going to rotate is (0,1,0). In your first case you most likely applied a rotation around the x axis. Sin(45°) as well as Cos(45°) are both 0.707 (actually 1/Sqrt(2) or Sqrt(0.5)). So when rotating (0,1,0) around x(1,0,0) by 45° we get (0, 0.707, 0.707). Projecting down we just drop the z component and get (0, 0.707, 0).
<br>
However now before we project down we further rotate our (0, 0.707, 0.707).vector around y (0,1,0) also be 45° This will lead to (0.5, 0.707, 0.5) To check we can calculate the length of this vector and it is still 1. It's just Sqrt(0.5² + 0.707² + 0.5²) == Sqrt(0.25 + 0.5 + 0.25) == Sqrt(1) == 1.
<br>
Of course when you project down that vector (again just dropping the z component) we get (0.5, 0.707, 0). Though i'm not quite sure what's unintuitive about that result.Thu, 09 Aug 2018 16:41:35 GMTBunny83Comment by MariusRu on MariusRu's answer
http://answers.unity.com/comments/1540385/view.html
I have a more general question on this topic: when I rotate my direction vector 45° relative to the plane, the Vector3.ProjectOnPlane is, as expected (0, 0.71, 0). However, when I rotate my direction vector 45° relative to the plane in **two** dimensions, I get (0.5, 0.71, 0). So although the rotation is the same in both dimensions, the length of the projected vector is not the same in the two dimensions. To me, this is counter-intuitive.
I guess this has more to do with basic geometry understanding, but can anyone point me in the right direction?Thu, 09 Aug 2018 15:34:36 GMTMariusRuComment by unityBerserker
http://answers.unity.com/comments/1461895/view.html
Here you find visualizations of Vectors and much more:
https://github.com/uvivagabond/Visualization-of-physics-in-Unity
It's easier to see then explain.Tue, 30 Jan 2018 12:10:02 GMTunityBerserkerComment by Harinezumi
http://answers.unity.com/comments/1456989/view.html
`Vecto3.ProjectOnPlane()` defines the plane by its *normal*, i.e. the vector perpendicular to it. The normal Vecto3.up defines the XZ plane.<br>
If you want to show the upward pointing component of the jump vector, then you need to project onto the jump horizontal direction, i.e. `Vector3 jumpDirection = new Vector3(jumpspeed.x, 0, jumpspeed.z).Normalize();`<br>
I'm also wondering, isn't what you are looking for simply `new Vector3(0, jumpspeed.y, 0)`?Fri, 19 Jan 2018 12:15:08 GMTHarinezumiAnswer by Bunny83
http://answers.unity.com/answers/1456975/view.html
Yes "ProjectOnPlane" simply projects the vector onto the plane that is perpendicular to the normal vector.
<br>
This line (due to simply using Vector3.up):
Vector3 project = Vector3.ProjectOnPlane(jumpspeed, Vector3.up);
would be the same as:
Vector3 project = jumpspeed;
project.y = 0;
So for trivial cases where the plane normal is one of the world axis / the target plane is identical with one of the 3 base planes it becomes quite easy to understand what actually happens. So since your normal is (0, 1, 0) the target plane is the X-Z plane.
<br>
Also keep in mind that the two vectors have to be in the same space. So either both vectors are worldspace directions or both a relative vectors of the same local space. Vector3.up doesn't have a particular space. It depends on the interpretation. So if you have a worldspace velocity vector and Vector3.up, that would mean Vector3.up is equivalent to the worldspace up direction. The resulting vector would be a worldspace vector
However when you have a local space direction and Vector3.up, the up vector would also be a localspace vector (the blue axis of the gameobject). Of course the resulting vector will also be a local space vector in this case.
<br>
Like Hellium said in the comment if the resulting vector doesn't have any x component your incoming vector doesn't have one either.
<br>
***edit***
Just for reference:
ProjectOnPlane simply does:
public static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal)
{
return vector - Vector3.Project(vector, planeNormal);
}
[and Project does][1]:
public static Vector3 Project(Vector3 vector, Vector3 onNormal)
{
float num = Vector3.Dot(onNormal, onNormal);
if (num < Mathf.Epsilon)
return Vector3.zero;
else
return onNormal * Vector3.Dot(vector, onNormal) / num;
}
So what actually happens is that your velocity vector gets projected onto the given normal vector. So the result of Project will be only the y-component of your velocity vector. This amount will be subtracted from your original velocity which only leaves the X and Z components. "num" is just the squared length of the normal and used to normalize the result in case the normal vector wasn't normalized. So in your case Vector3.up is a normalized vector and num would be "1"
[1]: https://docs.unity3d.com/ScriptReference/Vector3.Project.htmlFri, 19 Jan 2018 11:56:39 GMTBunny83Comment by Hellium
http://answers.unity.com/comments/1456942/view.html
Are you sure `jumpspeed` is not simply on YZ plane only and does not have any value on X axis? If so, projecting onto the XZ plane results in no value in X axis.Fri, 19 Jan 2018 10:47:59 GMTHellium