var gunShot : AudioClip;
var emptyClip : boolean = false;
function Update () {
if(Input.GetButtonDown(“Fire1”)) {
if (ammo > 0) {
Shoot();
}
else{
return;
}
}
if (Input.GetKeyDown(KeyCode.R)) {
if (emptyClip){
Reload();
}
}
if(ammo >= 0) {
emptyClip = true;
}
else {
emptyClip = false;
}
}
function Shoot() {
var bullet = Instantiate(bulletPrefab, transform.Find(“BulletSpawn”).position, transform.Find(“BulletSpawn”).rotation);
bullet.rigidbody.AddForce(transform.forward * bulletSpeed);
audio.PlayOneShot(gunShot);
ammo --;
}
function Reload() {
ammo = 40;
}
function OnGUI() {
GUI.Label(Rect ( 0, 0, 75, 25), "Ammo " + ammo);
}
What am I doing wrong here.
Most likely that your bullet pivot is inverted or the “BulletSpawn” point is inverted.
Or there is a local/global rotation/direction issue. There are a lot of unknowns and the biggest one to me is which transform are you using forward on.
Make sure that the “Z axis” os the bulletSpawn is pointing outwards/forward otherwise if it is facing inwards/backwards , it will obviously fire backwards.
the bullet and bulletspawn are all facing the right direction, i think the bullet is moving forward in the world, which then it is going in the right direction i need it to go the opposite way.
What Gameobject is this script attached to? That is the transform you are using to determine the direction of the force applied to the bullet.
Also, please use code tags when posting code - Its a simple step that makes it a lot easier for us to help.
http://forum.unity3d.com/threads/143875-Using-code-tags-properly
var bullet = Instantiate(bulletPrefab, transform.Find("BulletSpawn").position, transform.Find("BulletSpawn").rotation);
bullet.rigidbody.AddForce(transform.forward * bulletSpeed);
These two lines are the most interesting part.
- you are using the bulletSpawn object as your position and rotation. This is of little consequence though.
- You are adding force in “transform.forward” This is whatver the transform is that is there.
Assuming that your bullet is facing the right direction, and forward on the bullet is Z positive. (unity forward) Then you should change the second line to this:
bullet.rigidbody.AddForce(bullet.forward * bulletSpeed);
Also, do note that bulletSpeed MUST be positive. if it is negative, your bullet will go backward.
I know it doesn’t pertain to your issue… these guys got you on the right track… the part that stuck out at me as I read through was your lines:
if(ammo >= 0) {
emptyClip = true;
}
…seems as long as your clip does have bullets you say it’s empty and reload it every frame that you fire… basically you have infinite ammo or constant full clip on.