- Home /
Find a point with Vector and magnitude?
Hi,
i would like to find a point with a vector (starting point) and a magnitude, but i am a bit confused :
i throw a sphere up in the air, and when it starts coming down, i keep the position and set "y" to 0. (my new vector)
Then i would like to start from this new Vector, and add the same magnitude as from the old starting point and this new vector.
But when i instantiate a new cube at this destination point, this is not working, the cube is in the air, and i think i misunderstood something, how to calculate a vector and a magnitude to find another vector :
function Update () {
if (startOff){
bestY = transformR.position.y;
if (oldY < bestY) oldY = bestY;
else if (oldY > bestY && !vectorAlreadyFound){
vectorMiddlePoint = Vector3(transformR.position.x, 0, transformR.position.z);
pref = Instantiate(Resources.Load("pref", GameObject));
pref.transform.position = vectorMiddlePoint;//WORKS WELL
distWithMiddlePoint = (vectorMiddlePoint - startingPoint).magnitude;
//DESTINATION POINT (vector & magnitude)
pref2 = Instantiate(Resources.Load("pref", GameObject));
//pref.transform.position = transformR.forward * distWithMiddlePoint;
pref.transform.position = (vectorMiddlePoint - startingPoint) * distWithMiddlePoint;
vectorAlreadyFound = true;
}
}
Would you know how to achieve this?
SOLUTION (thanks to you guys) :
pref2.transform.position = ((vectorMiddlePoint - startingPoint).normalized * distWithMiddlePoint) + vectorMiddlePoint;
pref2.transform.position.y = 0.0;
Thanks
Answer by Democre · Nov 23, 2012 at 01:58 AM
I think you would need to do something like:
pref2.transform.position = ((vectorMiddlePoint - startingPoint).normalized * distWithMiddlePoint) + startingPoint;
You are then getting a unit vector in the desired direction, multiplying it by the desired length, and then adding that to the starting point.
@Democre you are perfectly right, thanks a lot for your answer, i completely forgot about the offset at the end. I added the new vector and y=0 (i don't know why it did not do it automatically, my edit in the first post). Thanks again!
Answer by Loius · Nov 23, 2012 at 01:17 AM
You're using pref instead of pref2 at the end, there.
@Loius thanks, nice, i was a bit tired i think, so the cube appears way too far from where it should be, i would like to take the same direction, and add the magnitude, could you help me with that?
If you want such a thing as:
o-(X units)-m-(X units)-b
Where O $$anonymous$$ and B are Vector3, and O is the origin, $$anonymous$$ is the sphere (midpoint), and B is the new cube's position, then:
b = m + (m-o);
Answer by Loius · Nov 23, 2012 at 02:56 AM
It sounds like you're trying to do this:
"While standing on an infinite, flat plane, you throw a ball from Point Zero, forward and into the air. When the ball reaches the apex of its curve, it is assumed that it has moved half as far relative to the plane as it shall move before landing. Place a box at the ball's landing point."
If that's the case, then:
Your midpoint is equal to (flattenedApex - origin). To get the (vertically) flattened apex, you just set its .y component to the origin's .y component. I think, with your variables, that's vectorMiddlePoint.y = startingPoint.y
The landing point is that far again from the midpoint: targetPoint = flattenedApex + (flattenedApex - origin);
To use a magnitude, you need a concept of "direction" (a vector can represent either a position or a direction, but not both [except when engineered to; hush]). The 'direction' in this case is (flattenedApex-origin); normalizing that allows you to scale it to the desired length, but check how funny it looks:
targetPoint = origin + (flattenedApex-origin).normalized (flattenedApex-origin).magnitude 2.0;
Thanks for the explanation, issue solved, thanks for the answers
Your answer
![](https://koobas.hobune.stream/wayback/20220613084349im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Moving a Platform 1 Answer
Why using = isn´t fast enough for changing Vector3 values from the transform? 2 Answers
Transformations 2 Answers
gravity causes transform.position not to work 0 Answers
Player rotation = Camera Rotation 0 Answers