i made a simple turret and placed it in my game.
it will track the target but for some reason will not fire the bullet prefab
which works with other models in the game. can someone help me out and see whats wrong with the script.
thank you very much.
/ find target
var find: Transform;
//gets bullets from prefab
var ammoPrefab: Transform;
//tells unity next time to fire
private var nexTime : float;
//delay between shots
var Delay : float;
function Update () {
//follows the target
transform.LookAt(find);
//checks to see if its time to fire
if(Time.time > nexTime){
//Blast!!
shoot();
//updates time
nexTime = Time.time + Delay;
}
}
//Weapon firing
function shoot (){
var ammo =
Instantiate(ammoPrefab,transform.find(“GameObject”).transform.position,
Quaternion.idenity);
ammo.rigibody.AddForce(transform.forward * 1000);
}
Please mark you script and use the format button (101010) so that the code snippet is formatted as a code snippet and is made readable.
you mispelled rigidbody to rigibody. And the second argument of your instantiate looks weird. You could add an empty game object as spawn point and place it wher you want the projectile to appear, then use GameObject.FindWithTag ("SpawnPoint").transform.position; and yes please use the 101010 button
Your script is a nightmare to read. Here is something I happened to have kicking around on my desktop. This will do the job for you but having said that this is an old script from when i was fairly new to Unity. You just need to make a bullet prefab (which you have set up as ammoPrefab). And as Fafase said you might want to make an empty game object as your gunTip. I don’t normally post code for people, but you seemed close enough.
var player : Transform;
var safeDist : float = 15;
var currentDist : float;
var shooting : boolean = false;
var bulletFab : Rigidbody;
var gunTip : Transform;
var power : float = 1000;
var shotDelay : float = 1;
function Update () {
currentDist = Vector3.Distance(transform.position, player.position);
if(currentDist < safeDist){
transform.LookAt(player);
if(!shooting) shootStuff();
}
}
function shootStuff(){
shooting = true;
var fwd = transform.TransformDirection(Vector3.forward);
var bulletShot : Rigidbody = Instantiate(bulletFab, gunTip.position, gunTip.rotation);
bulletShot.AddForce(fwd * power);
yield WaitForSeconds(shotDelay);
shooting = false;
}
Please mark you script and use the format button (101010) so that the code snippet is formatted as a code snippet and is made readable.
– GuyTidharyou mispelled rigidbody to rigibody. And the second argument of your instantiate looks weird. You could add an empty game object as spawn point and place it wher you want the projectile to appear, then use GameObject.FindWithTag ("SpawnPoint").transform.position; and yes please use the 101010 button
– fafase