x


turret AI question

hey everyone! so.. I'm having a problem... I'm making a game of batleships, and i is going very well so far, but now I wanted to make an enemy shoot at me, so i've already make the boat look at me but now i cant make it shoot! this is my script can anyone tell me what is wrong with it?

var bullitprefab: Transform;

shoot();

function shoot(){

var bullit = Instantiate(bullitprefab, transform.Find("SpanwPoint").transform.position, Quaternion.Identity);



bullit.Rigidbody.AddForce(transform.right * 1000);

}

this should be this be enought, right?

more ▼

asked May 24 '12 at 09:03 AM

ruifernandes01 gravatar image

ruifernandes01
23 4 8 14

It should, but what is calling the Shoot function?

May 24 '12 at 09:24 AM asafsitner
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Well nothing is really wrong with it but the variable bullitprefab has to be rigidbody. Let me give you an example on how I would use this, and explain some stuff:

var bulletprefab : Rigidbody;//has to be rigidbody to fire, and made it "bullet" instead of "bullit"
var spawn : Transform;

function Update(){

if(Vector3.Distance(transform.position, Transform.Find("Player")) < 10){   
//if the distance between player and enemy is beneath 10 do the following:

shoot();
}
}

function shoot(){

 var bullet = Instantiate(bulletprefab, spawn.position, transform.rotation);



bullet.rigidbody.AddForce(transform.right * 1000); // had to be rigidbody notRigidbody

}
more ▼

answered May 24 '12 at 04:31 PM

Hybris gravatar image

Hybris
415 5 8 10

tnx a lot Thomas, but now i have this error when i'm compiling it:

expecting ( found 'shoot', when you crate a new function.. any idea of wht it might be?

Jun 18 '12 at 07:59 PM ruifernandes01

Edited my answer, and by some reason my nickname won't show.... well that's my problem.

Jun 23 '12 at 07:36 PM Hybris

i'm still with the same problem, in line twelve..

Jun 23 '12 at 08:33 PM ruifernandes01

oh srry, for some reason it didn't update, it should work now. add at the end of update function an {

remove the last } of the shoot function, srry my mistake it's hard to throw in code

Jun 24 '12 at 07:40 AM Hybris

no problem! :) now there is an problem on :

if(Vector3.Distance(transform.position, transform.Find("Player")) < 10){

Jun 24 '12 at 01:05 PM ruifernandes01
(comments are locked)
10|3000 characters needed characters left

hey, thanks for your availability! I now have some errors like:

alt text

can you "inlightme" one more time? plz

Unity erros.png (124.5 kB)
more ▼

answered May 24 '12 at 09:07 PM

ruifernandes01 gravatar image

ruifernandes01
23 4 8 14

last } too much, srry will update my answer right away!...stupid me......thats like very stupid of me...cant believe it..

BTW plz make a comment instead of an answer

oh and unity acts weird if you still have the error of var not expected do this:

var bullet : Rigidbody; bullet = Instantiate(bulletprefab, spawn.position, transform.rotation);

It's just the long way around, sometimes unity does this I don't know why

May 25 '12 at 06:31 PM Hybris
(comments are locked)
10|3000 characters needed characters left

I'm kind of a newbie in scripting... i've created my function, one called Shoot.right?

more ▼

answered May 24 '12 at 02:59 PM

ruifernandes01 gravatar image

ruifernandes01
23 4 8 14

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5068
x957
x172

asked: May 24 '12 at 09:03 AM

Seen: 622 times

Last Updated: Jun 25 '12 at 09:09 PM