Turret Scripting

Hi guys so I have had many problems with this turret scripting thing and all I want it to do so far is to shoot one bullet every second or so at my main character. Here is what I have had so far:

The problem is obvious. if anyone has any alternatives to the script please tell me.
This is the script: (Look at video before replying, thanks)

var LookAtTarget:Transform;
var damp = 6.0;
var bullitPrefab:Transform;
var savedTime=0;

function Update ()
{
if(LookAtTarget)
{
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);

transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);

var seconds : int = Time.time;
var oddeven = (seconds % 2);

if(oddeven)
{
Shoot(seconds);
}
}
}

function Shoot(seconds)
{
if(seconds!=savedTime)
{
var bullit = Instantiate(bullitPrefab ,transform.Find(“spawnPoint”).transform.position ,
Quaternion.identity);

bullit.Rigidbody.Addforce(transform.forward * 1000);

savedTime=seconds;
}
}

I had this same problem, I fixed it by having a boolean that when 2 seconds (or however long your turret waits till the next bullet) are up, it equals true, the bullet shoots, and in the script right before the instantiate part of the script you have the boolean turn false, that way only one bullet is shot.

Do you think you could post the script for it. thanks

I edited your script with the boolean, this is untested but should work.

var LookAtTarget:Transform; 
var damp = 6.0; 
var bullitPrefab:Transform; 
var savedTime=0; 
var active : boolean = true;

function Update () 
{ 
if(LookAtTarget) 
{ 
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position); 

transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp); 

var seconds : int = Time.time; 
var oddeven = (seconds % 2); 

if(oddeven) 
{ 
active = true;
Shoot(seconds); 
} 
} 
} 

function Shoot(seconds) 
{ 
if(seconds!=savedTime) 
{ 
if(active){
active = false;
var bullit = Instantiate(bullitPrefab ,transform.Find("spawnPoint").transform.position , 
Quaternion.identity); 

bullit.Rigidbody.Addforce(transform.forward * 1000); 

savedTime=seconds; 
}
} 
}

I get an error from this and no shots come out of the barrel… but it still follows my character

Hmm… odd, I’ll take a look and see if I can find the problem.

Ok thank you, if you need ill pop a video onto youtube if you wanna see whats happening

Ok, just figured it out, I forgot that active is a command in unity and when used as a boolean causes an error because Unity doesn’t know what your doing with it, if you just change the name of the boolean to like activeShot, or if you add anything to it so that it’s not just the word active it should work.

Hmm i changed the var to activeShot but I still get a an error ^^ sorry this is a waste ur time a bet ^^ but could you put up the fixed scripted again… thanks allot

See this is what I changed it to… and now it shoots every second but its like the video I showed and it shoots bursts of about 100 every shot it does: Is this the correct script?

var LookAtTarget:Transform;
var damp = 6.0;
var bullitPrefab:Transform;
var savedTime=0;
var activeShot : boolean = true;

function Update ()
{
if(LookAtTarget)
{
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);

transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);

var seconds : int = Time.time;
var oddeven = (seconds % 2);

if(oddeven)
{
activeShot = true;
Shoot(seconds);
}
}
}

function Shoot(seconds)
{
if(seconds!=savedTime)
{
if(activeShot){
activeShot = false;
var bullit = Instantiate(bullitPrefab ,transform.Find(“spawnPoint”).transform.position ,
Quaternion.identity);

bullit.Rigidbody.Addforce(transform.forward * 1000);

savedTime=seconds;
}
}
}

Oh whoops, didn’t see your post, hmm… that’s odd, that it’s still doing that. Maybe if instead you make a timer function which is seperate, like this. this should work if you replace your previous timer system with it.

var LookAtTarget:Transform; 
var damp = 6.0; 
var bullitPrefab:Transform; 
var Shot : boolean = true; 

function Update () 
{ 
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position); 

transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp); 

if(Shot){ 
Shot = false; 
var bullit = Instantiate(bullitPrefab ,transform.Find("spawnPoint").transform.position , 
Quaternion.identity); 

bullit.Rigidbody.Addforce(transform.forward * 1000); 
Timer();
} 
}
function Timer(){
WaitForSeconds(1);
Shot = true;
}

Can you think of anything else? It would be very nice cause I need to figure this out so that I can continue my tutorials.

I edited my post with a script that should solve your problem, if it doesn’t I’m sure I’ll think of something else to try.

Well it stoped shooting many at once… but now I have to pres a button to shoot it? shouldnt it be automatic… also the bullets dont go anywere they just stack ontop of each other. Thanks in advance for all ur help ^^

If it helps im trying to follow the TornadoTwins tutorial part #11 Here is the youtube video Skip to about 7:00 min into it to get the final code… just w8 for him to finish typing it in.

There was a weird if statement in there, I edited it again, it should work fine now.

Still does the same as b4… im uploading a video as we speak ill put the link up when its done in about 1 min

LINK TO VIDEO… see if you can fix this… it should be auto-matic turret and it should actually shoot… not just in front of it. thanks](http://www.youtube.com/watch?v=uLkQvrlExL0)

Oh well im going to go eat dinner so ill be gone for a bit… just post a few different ideas and when i come back I will try em out… Thanks for all your help soi far I hope we can get it to work soon.

Ok, I went ahead and wrote some code from scratch, this does exactly what you need it to I believe, I tested it out and it shoots once every second at the target.

var projectile:Rigidbody;
var bulletShot : boolean;
var target : Transform;

function Start () {
bulletShot = true;
}

function Update () {
transform.LookAt(target);
if (bulletShot == true){
var instantiatedProjectile:Rigidbody =
Instantiate(projectile, transform.position, transform.rotation);
instantiatedProjectile.velocity = transform.forward * 125 * 1;
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.collider);
bulletShot = false;
ShotTimer();
}
}

function ShotTimer(){
yield WaitForSeconds(1);
bulletShot = true;
}