- Home /
How to find an intercept on a moving target
So here is my code roughly
Distance = Vector3.Distance(transform.position,Target.transform.position);
float timeToTarget;
float burnDistance = Projectile.MetersPerSecond * 2f * 2f * 0.5f;
if (Distance < burnDistance)
{
timeToTarget = Mathf.Sqrt(2 * Distance / Projectile.MetersPerSecond);
}
else
{
float velocity = Projectile.MetersPerSecond * 2;
TimeToTarget = 2 + (Distance - burnDistance) / velocity;
}
AimPoint = Target.transform.position + (Target.transform.rigidbody.velocity * TimeToTarget) + (Random.insideUnitSphere * Accuracy);
Distance = Vector3.Distance(transform.position,AimPoint);
TimeToTarget = Mathf.Sqrt(2 * Distance / Projectile.MetersPerSecond);
i'm trying to intercept a target with a projectile.
The issue is finding the proper time to target I think.
Basically when I find the distance to target then use that to find the time to target it changes my aimpoint to where the target will be in time seconds. But now the distance has changed. Now i'm now longer distance to target away i'm distance to aimpoint away from what i want to hit.
Basically as the target moves away from me it takes longer for the projectile to hit the aimpoint than is predicted by distance to target. So I can fix this somewhat by running the algorithm again. Only using the aimpoint as the target and getting a closer approximation. I could keep doing this to get really close but that seems highly ineffiecient. Is there a better way?
Please revisit your open questions and accept the answers you've been given. You seem to abandon a lot of your questions as soon as you have your answers. Either accept an answer or state what is still missing to accept an answer, don't let them open forever if they have been answered
I checked my own profile i have a total of 62 questions 10 of which actually had any answers at all and werent marked answered. one of which is this question posted 7 months a go and recieved no reply until 5 days ago which explains why it is isnt answered. another of which was a failure simply to mark my own posted answer reply as the answer and of the remaining 8 i checked them and dont view most of them as having been a reply that actually answers the question. I'd have to self post a reply and mark myself as answered (which I do on occasion thats why of my 5 pages of replies the last 3 and a half are all marked answered) but to suggest that what amounts to 4 or 5 unmarked questions is abandoning most seems extreme. If I abandon questions at all its only because i don't ask questions with simple 1 code line replies. Often I dont get replies because of the complexity of the question.
You're right, "a lot" was way out of proportions. Let me rephrase: If the last action on one of your questions was an answer, common decency dictates that you at least reply to the guy that took some time to try to answer and tell him why this answer can't be accepted. I actually only bothered to comment here, because I considered trying to answer a question of yours, but then saw some old blue questions where you got an answer and then never replied or accepted. Anyway, I see you've gone back and accepted at least one old one I've seen, so that's good...
Answer by Bunny83 · Jan 12, 2014 at 05:38 PM
This question has been asked around 5 times here on UA:
http://answers.unity3d.com/questions/296949/how-to-calculate-a-position-to-fire-at.html
Answer by Mutain · Jan 06, 2014 at 10:41 PM
take a look at this post http://stackoverflow.com/questions/2248876/2d-game-fire-at-a-moving-target-by-predicting-intersection-of-projectile-and-u