- Home /
My bullets just fall to the ground
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;
}
,
Answer by YoungDeveloper · Oct 05, 2013 at 09:24 AM
Add more force, like 4000. If its falling through ground something is wrong with the colliders. If the ground has mesh collider attached, tick convex for bullet.
Thank you, but there's one thing; my bulletspawn doesn't follow my gun, but it is still attached.
I've tried that, but it doesn't work. The spawn at the same place all the time.
You could show us a picture of your objects hierarchy.
Your answer
Follow this Question
Related Questions
how to move my camera... 2 Answers
Mouse-following script freezes Unity. 1 Answer
AddForce to the Left? 3 Answers
Random placement objects. 0 Answers