Search Unity

manchine gun problem?

Discussion in 'Scripting' started by tyronendrw, Aug 19, 2009.

  1. tyronendrw

    tyronendrw

    Joined:
    Jul 22, 2009
    Posts:
    18
    am new to :arrow: codeing....but i understand a little. the problem that i have is that i attach this script to ever gun i have and the bullets go under the gun when shooting and am trying to add ammo code to this. i attach the script. if you need anything else plz feel free to ask. thanks in advance!
     

    Attached Files:

  2. Jim Offerman

    Jim Offerman

    Joined:
    Jul 17, 2009
    Posts:
    177
    If you ignore the accuracy randomness for a moment, you'll see that the world-space direction of the raycast in FireOneShot() is basically always (0, 0, 1). That can't be right! Try using transform.parent.forward instead.

    (I'm saying transform.parent. forward instead of transform.forward, because you're shooting from the parent transform's position as well)

    Another bug I noticed is in LateUpdate(), where this check:

    Code (csharp):
    1.  
    2. if (muzzleFlash || lightFlash)
    3.  
    should actually be this:

    Code (csharp):
    1.  
    2. if (muzzleFlash  lightFlash)
    3.  
    Your current version will result in a null reference exception, if only one of these references is valid.
     
  3. tyronendrw

    tyronendrw

    Joined:
    Jul 22, 2009
    Posts:
    18
    this script is from fps...but it does the same thing when attach it to weapon it :arrow: shoots under the gun. i change the script a bit to fit my needs.plz help

    thanks in advance
     

    Attached Files:

  4. Jim Offerman

    Jim Offerman

    Joined:
    Jul 17, 2009
    Posts:
    177
    Ah, I see your problem now.

    The script assumes that transform.position is the position you are firing from. Most likely, this is not the case with your gun model. Two ways to solve this problem:

    (a) move the gun model in your modeling package so that the muzzle (the point where the bullet comes out) ends up at (0,0,0). Save and then re-import in Unity.

    (b) keep the model as is, but find out the local-space coordinates of the muzzle. Lets call that point M. Then, in FireOneShot() where you build your ray use transform.TransformPoint(M) instead of transform.position.

    hth,
    Jim.
     
  5. tyronendrw

    tyronendrw

    Joined:
    Jul 22, 2009
    Posts:
    18
    thanks works great ;D