I am trying to fire a bullet forward from a transform called BarrelEnd, but my bullets just spawn at BarrelEnd and they fall to the ground, how do I fix this?
Here’s my script:
var AmmoInPocket = 240;
var AmmoInCurrentMag = 30;
var MuzzleFlash : GameObject;
var MuzzleLight : Light;
var RoundBursting = false;
var ReloadAudio : GameObject;
var Cam : Camera;
var BarrelEnd : Transform;
var Bullet : Rigidbody;
var HipPose : Vector3;
var AimPose : Vector3;
function Start () {
transform.localPosition = HipPose;
}
function Update () {
if(AmmoInPocket < 0 ){
AmmoInPocket = 0;
Debug.Log("Player has no more ammo for ACW-R");
}
//while(RoundBursting){
//MuzzleFlashFunc();
//MuzzleLightFunc();
//}
if(Input.GetKeyDown(KeyCode.R)){
if(AmmoInCurrentMag < 30 && AmmoInCurrentMag > -1 && AmmoInPocket > 0){
animation.Play("ReloadACW-R");
AmmoInCurrentMag = 30;
AmmoInPocket -= 30;
ReloadAudio.audio.Play();
}
}
if(Input.GetButton("Fire2")){
transform.localPosition = AimPose;
Cam.fieldOfView = 40;
}
if(!Input.GetButton("Fire2")){
transform.localPosition = HipPose;
Cam.fieldOfView = 60;
}
if(Input.GetButtonDown("Fire1")){
var BulletPrefab : Rigidbody;
BulletPrefab = Instantiate(Bullet, BarrelEnd.position, BarrelEnd.rotation) as Rigidbody;
BulletPrefab.AddForce(BarrelEnd.forward * 40);
if(AmmoInCurrentMag > 0){
AmmoInCurrentMag -= 1;
AmmoInPocket -= 1;
audio.Play();
MuzzleFlashFunc();
MuzzleLightFunc();
}
}
}
function MuzzleLightFunc () {
MuzzleLight.enabled = true;
yield WaitForSeconds(0.04);
MuzzleLight.enabled = false;
}
function MuzzleFlashFunc () {
MuzzleFlash.renderer.enabled = true;
yield WaitForSeconds(0.04);
MuzzleFlash.renderer.enabled = false;
}
,