Target lead (aim ahead) problem
Hi, trying to upgrade my turret asset, but target lead is giving me problems. Its just not working.
After days on-off googling I came down with the following few methods I found the clearest. But both just return the exact target position, not an estimate at all. At least for me. Can anyone confirm they work? I didn't find any other part of my code that replaces the result. The default bullet speed is 10.
Example 1:
Vector3 targetVelocity = target.GetComponent<Rigidbody>().velocity;
float distance = Vector3.Distance(turretHead.position, targetPos);
float travelTime = distance/bulletSpeed;
Vector3 aimPoint = target.position +targetVelocity*travelTime;
float distance2 = Vector3.Distance(turretHead.position, aimPoint);
float travelTime2 = distance2/bulletSpeed;
estPos = target.position +targetVelocity*travelTime2;
Example 2
Vector3 targetVelocity = target.GetComponent<Rigidbody>().velocity;
float distance = Vector3.Distance(targetPos, turretHead.position);
Vector3 firstPassV = targetPos + ((targetVelocity*1.15f)*(distance/bulletSpeed));
float firstPassD = Vector3.Distance(firstPassV,turretHead.position);
Vector3 secondPassV = targetPos + ((targetVelocity*1.15f)*(firstPassD/bulletSpeed));
float secondPassD = Vector3.Distance(secondPassV,turretHead.position);
estPos = targetPos + ((targetVelocity*1.15f)*(secondPassD/bulletSpeed));
Example 3:
Vector3 newPos = targetPos;
Vector3 newDir = target.GetComponent<Rigidbody>().velocity;
float dis;
if(muzzlePositions.Length > 0)
dis = (newPos - muzzlePositions[randBarrel].position).magnitude;
else
dis = (newPos - turretHead.position).magnitude;
estPos = newPos + (dis/bulletSpeed)*newDir;
This is a screen of whats happening. Cube is moving at speed of 2, in circular motion, and bullet at speed of 10. As you can see with raycast, the result always points at the exact position of the target, not an estimate. I must be doing something wrong if 3 different methods give me the same result, I just don't know what.
Answer by ChrisSch · Nov 28, 2015 at 11:15 PM
Sigh, I'm an idiot. :D I've been moving the target with transform.Translate, which does not produce velocity. Almost 95% of the time I move objects with Addforce or directly changing velocity, so I sort of forgot how transform.Translate works... Changed it to rigidbody.MovePosition and it works now. Only tested Example 3 though.