- Home /
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?
Answer by Hybris · May 24, 2012 at 04:31 PM
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
}
tnx a lot $$anonymous$$, 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?
Edited my answer, and by some reason my nickname won't show.... well that's my problem.
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
no problem! :) now there is an problem on :
if(Vector3.Distance(transform.position, transform.Find("Player")) < 10){
Answer by ruifernandes01 · May 24, 2012 at 02:59 PM
I'm kind of a newbie in scripting... i've created my function, one called Shoot.right?
Answer by ruifernandes01 · May 24, 2012 at 09:07 PM
hey, thanks for your availability! I now have some errors like:
can you "inlightme" one more time? plz
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 ins$$anonymous$$d 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
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
trouble with my lookAt script 0 Answers
Trouble with turret script 1 Answer
AI using Javascript 1 Answer
Enemies Moving Through Terrain Help 1 Answer