I’m trying to make an object reach another. (Yeah this sound like something I could find in google first, I found many utils link but not what I wanted).
My object is in the floor and the target is flying,
The object is a rigidBody with gravity, and I want to set a velocity or call to AddForce, but I want to call addForce just one time, I’m using ForceMode.VelocityChange.
To get the vector destination I’m doing this:
Vector3 destination = targetPosition - playerPosition;
If I play with destination, multiplying by random number like 50, it’s work . (Of course more fast than I want)
I was trying to find tutorial’s on internet to understand the formulas for this, but I couldn’t .
Maybe because there is the gravity of unity, I’m getting confuse with the formulas I found on internet, or maybe is just me that I don’t understand.
So how can I calculate the vector that I need to reach the target, ?
Someone can help me , sorry for the newbie question.
if you are here, after long time trying to find something similar like me, here is the code:
Vector3 direction = new Vector3(hit.point.x, hit.point.y, 0f) - actualPosition;
float tan = (direction.y * 4 / (direction.x * 2));
float dg = Mathf.Atan(tan);
float velocity = Mathf.Sqrt(((direction.x * 2) * 9.81f) / (Mathf.Cos(dg) * Mathf.Sin(dg) * 2));
Vector3 v = new Vector3(Mathf.Cos(dg), Mathf.Sin(dg), 0f);
GetComponent<Rigidbody>().AddForce(velocity * v, ForceMode.VelocityChange);