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);
}
}
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
– Glenn_LuskBut the turret doesnt follow the enemy I see no problem with the logic in the script. Can you use a
– Chronos-LDebug.Log()in yourOnTriggerEnter()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
– Chronos-LOnTriggerEnter()with theDebug.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"
– Glenn_LuskThis is incorrect: Debug.Log(OnTriggerEnter); Try this: Debug.Log( "Trigger enter" );
– Chronos-L