Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

bullets are flying backwards when i shoot.

Discussion in 'Scripting' started by Jedeye, May 15, 2014.

  1. Jedeye

    Jedeye

    Joined:
    Oct 28, 2013
    Posts:
    2
    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.
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Most likely that your bullet pivot is inverted or the "BulletSpawn" point is inverted.
     
  3. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    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.
     
  4. zDemonhunter99

    zDemonhunter99

    Joined:
    Apr 23, 2014
    Posts:
    478
    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.
     
  5. Jedeye

    Jedeye

    Joined:
    Oct 28, 2013
    Posts:
    2
    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.
     
  6. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    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
     
  7. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Code (csharp):
    1.  
    2. var bullet = Instantiate(bulletPrefab, transform.Find("BulletSpawn").position, transform.Find("BulletSpawn").rotation);
    3. bullet.rigidbody.AddForce(transform.forward * bulletSpeed);
    4.  
    These two lines are the most interesting part.

    1. you are using the bulletSpawn object as your position and rotation. This is of little consequence though.
    2. 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:

    Code (csharp):
    1.  
    2. bullet.rigidbody.AddForce(bullet.forward * bulletSpeed);
    3.  
    Also, do note that bulletSpeed MUST be positive. if it is negative, your bullet will go backward.
     
  8. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    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:

    Code (csharp):
    1.  
    2. if(ammo >= 0) {
    3.    emptyClip = true;
    4. }
    5.  
    ...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.