- Home /
Glitchy Turret Script
Hey everybody. I have been working on a turret script, but it does not seem to be working properly. I have set up a series of booleans, I made it so that if the object tagged enemy enters the trigger Look is true and the turret would rotate around and fave the object. Also when the object left the trigger, it would stop looking. Then I tried to make it shoot as seen in the code below. But it will work(as in look and shoot as it is supposed to), or it wouldn't work (randomly stopping and then would stop shooting even when the object was still in the trigger). It really is hard to explain so please try it out in a unity scene. Here is the script(ignore the comments):
var Look : boolean = false;
var Target : Transform;
//var Dead : boolean = false;
//var Health : int = 10;
var damp : float;
var Projectile : Transform;
var fireRate = 5;
private var nextFire = .5;
function OnTriggerEnter(other : Collider){
if(other.gameObject.tag == "Enemy"){
Look = true;
}
}
function OnTriggerExit(){
Look = false;
}
function Update(){
if(Look){
var rotate = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
Shoot();
}
}
function Shoot(){
if(Time.time > nextFire){
nextFire = Time.time + fireRate;
var projectile = Instantiate(Projectile, transform.Find("Spawn").transform.position, Quaternion.identity);
projectile.rigidbody.AddForce(transform.forward * 1000);
}
}
Any help at all will be appreciated.
Answer by Major · Jul 26, 2012 at 09:20 PM
Aha! I fixed it. It was a problem with the OnTriggerExit. It needed similar things that the OnTriggerEnter used.
Answer by Seth-Bergman · Jul 26, 2012 at 03:32 AM
change OnTriggerEnter to OnTrigger
that should do it:)
It doesn't work. It won't look at anything unless I check the bool myself.
does one of the objects have a rigidbody? (either the turret or the enemy?)
enemy does have a rigid body. although use gravity is not checked
not kinematic is it? (that would be too easy..) then again that wouldn't matter anyway with trigger.. are you sure the tag is correct? double check, that's a common one too.. and isTrigger is checked on the turret?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
enable components from multiple objects 0 Answers
Scene Change Collision 2 Answers
Realtime Shadow Glitch [Unity Indie: Directional Light] 2 Answers
Problem with multiple triggers 0 Answers