Hello, I have a script I’m working on to make an AI turret lead a target.
It is finding the distance to the target fine, but the problem I’m having is that the velocityDist1 and velocityDist2 are coming out as the same number as distTarget. I don’t see the error thats causing this to happen though I imagine its something I have looked over that someone else may find easily.
Thank you for any feedback!
var Target : GameObject;
var barrel : GameObject;
var launcher : GameObject;
var projectile : Rigidbody;
var projectileSpeed = 10;
var turnSpeed = 5;
var accuracy = 1.0;
var weaponRate : float = 0.25;
private var cooldown : float = 0.0;
function FixedUpdate () {
if (Target) {
// actual distance to target
var distTarget = Vector3.Distance(Target.transform.position, transform.position);
Debug.Log("distTarget = " + distTarget);
// first calculation, using actual distance
velocityPosition1 = (Target.transform.position + ( (Target.rigidbody.velocity*accuracy) * (distTarget/projectileSpeed) ));
velocityDist1 = Vector3.Distance(velocityPosition1, transform.position);
Debug.Log("velocityDist1 = " + velocityDist1);
// second calc., using distance from first calc.
velocityPosition2 = (Target.transform.position + ( (Target.rigidbody.velocity*accuracy) * (velocityDist1/projectileSpeed) ));
velocityDist2 = Vector3.Distance(velocityPosition2, transform.position);
Debug.Log("velocityDist2 = " + velocityDist2);
// third calc., using distance from second calc.
TargetInterceptPosition = (Target.transform.position + ( (Target.rigidbody.velocity*accuracy) * (velocityDist2/projectileSpeed) ));
Debug.Log("TargetInterceptPosition = " + TargetInterceptPosition);
Debug.DrawLine(transform.position,TargetInterceptPosition, Color.cyan, 0);
// aim turret/ship at intercept position
rotationTarget = Quaternion.LookRotation((TargetInterceptPosition) - transform.position);
Debug.Log("rotationTarget = " + rotationTarget);
transform.rotation = Quaternion.Lerp(rotationTarget, transform.rotation, Time.deltaTime * turnSpeed);
if(Time.time > cooldown){
cooldown = Time.time + weaponRate;
Fire();
}
}
else {
Target = GameObject.FindWithTag("Blimp");//or find closest object in seperate function, etc.
}
}
function Fire(){
var shellClone : Rigidbody;
shellClone = Instantiate(projectile, launcher.transform.position, launcher.transform.rotation);
shellClone.velocity = launcher.transform.forward * projectileSpeed;
Physics.IgnoreCollision(shellClone.collider, collider);
audio.Play();
}