I've read many post on this matter and just about all of them refer to rigidbody.velocity. In my case i am not using rigidbodys or physics. What i am attempting to do is display a GUI that indicates where the player should shoot in order to hit the target. Here is what i have atm.
private var Player : Transform;
private var SelectedTarget : GameObject;
var TLI_HUD : GUITexture;
function LateUpdate(){
var TargetSpeed : float = SelectedTarget.GetComponent(AI).Speed;
var ProjectileSpeed : float = 200;
TLILead = ((transform.position - SelectedTarget.transform.position).magnitude * (TargetSpeed)) / ProjectileSpeed;
//Set GUI with this
TLI_HUD.transform.position=PlayerCamera.camera.WorldToViewportPoint(SelectedTarget.transform.TransformDirection(Vector3.forward) * TLILead);
//Or Set GUI with this
//TLI_HUD.transform.position=PlayerCamera.camera.WorldToViewportPoint(SelectedTarget.transform.position + (SelectedTarget.transform.forward * TLILead));
}
Any thoughts, ideas, or other solutions to make this work are welcome.
Your last line don't make much sense because you convert the offset vector from world into viewport space, but it's not a world space coordinate. You have to add at least the target position
The calculation of the exact interception course of two linear moving objects is quite heavy. I've done that one time in my life. The approximation you use will work if your projectile is a lot faster than the target you try to hit. You can improve the accuracy by repeating the calculation you did with the new target position. It will be only an approximation and you will never get the correct angle, but it comes quite close.
var TLI_HUD : GUITexture;
private var Player : Transform;
private var SelectedTarget : GameObject;
private var iterations : int = 3; // increase this to get a better result
private var ProjectileSpeed : float = 200;
function CalculateLead(target : Vector3, targetSpeed : Vector3) : Vector3
{
var dist : float = (Player.position - target).magnitude;
var timeToTarget : float = dist / ProjectileSpeed;
var newTargetPosition : Vector3 = SelectedTarget.transform.position + targetSpeed * timeToTarget;
return newTargetPosition;
}
function LateUpdate () {
var TargetSpeed : Vector3 = SelectedTarget.transform.forward * SelectedTarget.GetComponent(AI).Speed;
var aimTarget : Vector3 = SelectedTarget.transform.position;
for (var i = 0;i<iterations;i++)
aimTarget = CalculateLead(aimTarget,TargetSpeed);
TLI_HUD.transform.position=PlayerCamera.camera.WorldToViewportPoint(aimTarget);
}
Have you checked whether SelectedTarget.GetComponent(AI).Speed returns the right value? Also check the magnitude of (aimTarget - SelectedTarget.transform.position) to see if there's a difference. If every variable is assigned it should work. Can't test it at the moment (it's 4:40am here :D) maybe later.
@Bunny83 I got it all working. As it turns out i was accessing the Vector3 and rotation of a child object attached to the SelectedTarget, and it's rotations were not changing even though the parent was rotating. I made a few changes and came up with this, which works just fine for now.
Your last line don't make much sense because you convert the offset vector from world into viewport space, but it's not a world space coordinate. You have to add at least the target position
– Bunny83