My turrets do not follow the enemy!

UPDATED AGAIN: Here is my script. My turrets fire correctly! but still do not follow the enemy…

var myProjectile : GameObject;
var reloadTime : float = 1f;
var turnSpeed : float = 5f;
var fireSpeed : float = .25f;
var muzzleEffect : GameObject;
var errorAmount : float = .001;
var myTarget : Transform;
var muzzlePositions : Transform[];
var turretBall : Transform;
 
private var nextFireTime : float = .5f;
private var nextMoveTime : float = .5f;
private var desiredRotation : Quaternion;
private var aimError : float =.5f;
 
function start() {
 
}
 
function Update() {
 
if(myTarget) {
 
if(Time.time >= nextMoveTime) {
 
CalculateAimPosition(myTarget.position);
turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime*turnSpeed);
 
}
 
if(Time.time>= nextFireTime) {
 
FireProjectile();
 
}
}
}
 
 
function OnTriggerEnter(other : Collider) {
Debug.Log( "Trigger Enter");
if(other.GameObject.tag == "Enemy"){
 
nextFireTime = Time.time+(reloadTime*.5);
myTarget = other.GameObject.transform;
 
}
 
}
 
function OnTriggerExit(other : Collider) {
 
if(other.GameObject.transform == myTarget){
 
myTarget = null;
 
}
}
 
function CalculateAimPosition(targetpos : Vector3) {
 
var aimPoint = Vector3(targetpos.x+aimError, targetpos.y+aimError, targetpos.z+aimError);
desiredRotation = Quaternion.LookRotation(aimPoint);
}
 
function CalculateAimError() {
 
aimError = Random.Range(-errorAmount, errorAmount);
 
}
 
function FireProjectile() {
 
audio.Play();
nextFireTime = Time.time+reloadTime;
nextMoveTime = Time.time+nextFireTime;
CalculateAimError();
        
for(theMuzzlePos in muzzlePositions) {
 
 
Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
Instantiate(muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation);
 
}
}

3 Answers

3

You should use Time.time instead of Time.deltaTime. You use Time.deltaTime right only once in the Quaternion.Lerp()

function Update() { 
   if(myTarget) { 
      if(Time.time >= nextMoveTime) { 
         ...
      }
 
      ...
   }
}

function FireProjectile() {
   audio.Play();
   nextFireTime = Time.time + reloadTime;
   nextMoveTime = Time.time + nextFireTime;
   CalculateAimError();

   ...
}

The CalculateAimError() produce compile error because there is a syntax error there:

You wrote:

aimError = Random.Range[-errorAmount, errorAmount];

It should be:

aimError = Random.Range(-errorAmount, errorAmount);

I am very sure that this is a script from the Unity Cookie Tower Defense tutorial. You should have post your question there, or at the very least, try to pause the video and look for the difference between the tutorial script and your script.

I thought I did, and I actually really appreciate the input. Thanks for taking your time to help me ah that deltaTime was a deltatime. I see thanks much! But the turret doesnt follow the enemy

But the turret doesnt follow the enemy I see no problem with the logic in the script. Can you use a Debug.Log() in your OnTriggerEnter() to make sure that the trigger is properly setup?

@glennlusk, you should post comments, not answer; now your older answer/comment have overwrite your newer answer/comment. Can you provide a complete error message, and a code-snippet of the your OnTriggerEnter() with the Debug.Log()?

Ah it says NullReferenceException: Object reference not set to an instance of an object. But in unity I've set the target, and in the script editor I've Identified the "Enemy" and I've tagged my game object as "Enemy"

This is incorrect: Debug.Log(OnTriggerEnter); Try this: Debug.Log( "Trigger enter" );

private var nextFireTime : float;
private var nextMoveTime : float;
private var desiredRotation : Quaternion;
private var aimError : float;

i am not having much knowledge about javascript but are you sure the above thing will work??? you are directly passing datatype instead of the variable… so according to me it should be like below

private var nextFireTime : your variable name which is holding the value;
private var nextMoveTime : same here ;
private var desiredRotation : same here;
private var aimError : same here;

Don’t forget to mark the answer if found useful…cheers…

@maulik2208, I am pretty sure that it will work. In uJS, you can do either of the following: - var nextFireTime; - var nextFireTime : float; - var nextFireTime = 1f; - var nextFireTime : float = 1f;

@Chronos-L Thanks a lot buddy sharing this important knowledge with me..... as i don't have much knowledge about JS..... i thought might it could be a problem.....but anyways.....thanks again UPvoted your comment and answer......

Thanks guys this helped a little :)

Try this:

var target : Transform;

function Update () {
	
	// Rotate towards target    
	var targetPoint = target.position;
	targetPoint.y = transform.position.y;
	
	var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);
	transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
}