add one unit of distance from vector3 a to vector3 b
hello all:
so i have object a and object b, and what i want is to take object a position, take object b position and from object a draw a line (imaginary realy, not in unity) to point b and get a point x distance away in that direction.
i made a pic to clarify:
anyway thanks in advance.
[1]: /storage/temp/133836-sin-titulo.png
Answer by AaronBacon · Feb 27, 2019 at 12:27 AM
Its quite simple really, You just need to take both of the vectors, Get the difference between them, then add that difference to one of them. Eg:
Vector3 PointA = new Vector3(1, 2, 3);
Vector3 PointB = new Vector3(-5, 1, 2);
Vector3 PointC;
void Start () {
PointC = PointA+(PointA-PointB);
Debug.Log(PointC);
}
This Code Outputs:
(7.0, 3.0, 4.0)
Which if you do the math is correct for these values
thanks! it worked perfectly.
as a follow up question: how can i ADD to that vector3.
i tried multiplying and it works but im wondering if i can just add 1, 2 or 10 units of distance in that direction.
again, thanks for the replay, have a good day, :D
You can use Vector3.x for example, to get the individual elements of the Vector 3, eg:
PointA.x = PointA.x + 5 // Add 5 to the X component
PointB.y = PointB.y -10 // take 10 from Y Component
It's easier to get the individual components as floats or integers and manipulate them like that
Your answer
Follow this Question
Related Questions
Best videos or books for understanding vector math in unity 3d 1 Answer
Expression denotes a `type', where a `variable', `value' or `method group' was expected 2 Answers
Dictionary ContainsKey not working with Vector3s? 0 Answers
Creating a new vector along a known vector in the opposite direction of the known vector. 1 Answer
Calculating a Vector3 position waypoint 2 Answers