- Home /
How to cast Vector3 on a plane to get Vector2?
I'm preparing a cannon that shoots the player. It computes the initial velocity v_0 of missiles according to given position of player P and angle alpha. I need to cast the vector player.position - cannon.position on a plane which contains both cannon and player and is perpendicular to the ground.
I'm confused by your question. The vector player.position - cannon.position will be on the plane that contains both player and cannon. Therefore the 'cast' will intersect an infinite number of points on the perpendicular plane. Note that Unity's mathematical Plane class has a Raycast() function.
Answer by matir143 · Jul 07, 2014 at 01:25 AM
Ok well that was silly question here is anwser:
Vector3 d = t.position - transform.position;
float x_0 = Mathf.Sqrt(d.x*d.x+d.z*d.z);
float y_0 = d.y;
Vector2 c = new Vector2(x_0,y_0);
or
Vector3 d = t.position - transform.position;
float x_0 = Mathf.Magnitude(new Vector3(d.x,0,d.z));
float y_0 = d.y;
Vector2 c = new Vector2(x_0,y_0);
Answer by Oliver1135 · Jul 07, 2014 at 01:25 AM
it seems that the most simple way to get what you need here is to perform your Vector3 operator to get the vector between the two and then to just drop the z component and create a new Vector 2 from the X and Y of your Vector3
var vectorBetween = player.position - cannon.position;
var vec2 = new Vector2(vectorBetween.x, vectorBetween.y);
seems like the cost of creating a new Vector2D is practically negligable
Your answer
Follow this Question
Related Questions
Vector1, Vector2, Vector3? 1 Answer
Convert Vector2 to Vector3 based on the ground's height? 1 Answer
Vector2 Normalize ? 3 Answers
Vector2 vs Vector3 performance? 2 Answers
how do I find this point.. 1 Answer