Search Unity

FPS 1.13 Problem with BulletHoles

Discussion in 'Editor & General Support' started by i3artyy2222, Aug 21, 2013.

  1. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Hi,
    I have a problem with the bullet holes because when iam shooting it leaves bulletholes but sometimes not i dont know why :(
    edit:and i noticed that this happens at some angle.
    here is video of my problem
    http://www.youtube.com/watch?v=u2JEdUpGeOk
    Can anyone help me ??
    Thanks
    Regards,i3arty


    edit: is anyone on this forum ? can someone answer or something ???
     
    Last edited: Aug 24, 2013
  2. SlyRipper

    SlyRipper

    Joined:
    Jun 19, 2012
    Posts:
    251
    Show us your script/code so we can find the possible problem. maybe it's something in the calculation?
    Are you using raycast? would be easier to raycast and use the hit-position to place the bullethole-decal there, should have no effects on angle.
     
  3. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    BulletScript
    PHP:
    var maxDist float 1000000000;
    var 
    decalHitWall GameObject;
    var 
    floatInFrontOfWall float 0.00001;

    function 
    Update () 
    {
        var 
    hit RaycastHit;
        if (
    Physics.Raycast(transform.positiontransform.forwardhitmaxDist))
        {
            if (
    decalHitWall  hit.transform.tag == "Level Parts")
                
    Instantiate(decalHitWallhit.point + (hit.normal floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
        }
        
    Destroy(gameObject);
    }
    DestroyAfterTimeScript
    PHP:
    var destroyAfterTime float 30;
    var 
    destroyAfterTimeRandomization float 0;
    @
    HideInInspector
    var countToTime float;

    function 
    Awake ()
    {
        
    destroyAfterTime += Random.value destroyAfterTimeRandomization;
    }

    function 
    Update () 
    {
        
    countToTime += Time.deltaTime;
        if (
    countToTime >= destroyAfterTime)
            
    Destroy(gameObject);
    }
    GunScript
    PHP:
    var beingHeld boolean false;
    var 
    outsideBox GameObject;
    @
    HideInInspector
    var countToThrow int = -1;
    var 
    playerTransform Transform;
    var 
    playerMovementScript PlayerMovementScript;

    @
    HideInInspector
    var cameraObject GameObject;
    @
    HideInInspector
    var targetXRotation float;
    @
    HideInInspector
    var targetYRotation float;
    @
    HideInInspector
    var targetXRotationV float;
    @
    HideInInspector
    var targetYRotationV float;

    var 
    rotateSpeed float 0.3;

    var 
    holdHeight float = -0.5;
    var 
    holdSide float 0.5;
    var 
    racioHipHold float 1;
    var 
    hipToAimSpeed float 0.1;
    @
    HideInInspector
    var racioHipHoldV float;

    var 
    aimRacio float 0.4;

    var 
    zoomAngle float 30;

    var 
    fireSpeed float 15;
    @
    HideInInspector
    var waitTilNextFire float 0;
    var 
    bullet GameObject;
    var 
    bulletSpawn GameObject;

    var 
    shootAngleRandomizationAiming float 5;
    var 
    shootAngleRandomizationNotAiming float 15;

    var 
    recoilAmount float 0.5;
    var 
    recoilRecoverTime float 0.2;
    @
    HideInInspector
    var currentRecoilZPos float;
    @
    HideInInspector
    var currentRecoilZPosV float;

    var 
    bulletSound GameObject;
    var 
    muzzelFlash GameObject;

    var 
    gunbobAmountX float 0.5;
    var 
    gunbobAmountY float 0.5;
    var 
    currentGunbobX float;
    var 
    currentGunbobY float;

    var 
    gunModelObjects GameObject [];

    var 
    reloading boolean false;
    var 
    reloadAnimation Animation;
    var 
    reloadAnimationString String;
    var 
    reloadSound AudioSource;

    var 
    clipSize int 25;
    var 
    currentClip int 25;
    var 
    maxExtraAmmo int 100;
    var 
    currentExtraAmmo int 100;

    var 
    bulletHudTexture Texture;
    var 
    ammoCountRect Rect Rect(25,25,50,25);
    var 
    ammoStartX int 100;
    var 
    ammoY int 25;
    var 
    ammoSize Vector2 Vector2(10,25);
    var 
    ammoSpacing int 4;
    var 
    ammoDecorationHudRect Rect Rect(25,50,50,25);
    var 
    ammoDecorationTexture Texture;

    function 
    Awake ()
    {
        
    countToThrow = -1;
        
    playerTransform GameObject.FindWithTag("Player").transform;
        
    playerMovementScript GameObject.FindWithTag("Player").GetComponent(PlayerMovementScript);
        
    cameraObject GameObject.FindWithTag("MainCamera");
    }

     function 
    LateUpdate () 
    {
    if (
    currentClip clipSize)
        
    currentClip clipSize;
    if (
    currentExtraAmmo maxExtraAmmo)
        
    currentExtraAmmo maxExtraAmmo;
    if (
    currentClip 0)
        
    currentClip 0;
    if (
    currentExtraAmmo 0)
        
    currentExtraAmmo 0;
        
    if (
    beingHeld)
    {
        if (!
    reloading  Input.GetButtonDown('Reload')  currentClip clipSize  currentExtraAmmo 0)
        {
            
    reloading true;
            
    reloadAnimation.Play(reloadAnimationString);
            
    reloadSound.Play();
        }
        if (!
    reloading  Input.GetButtonDown('Fire1')  currentClip == 0  currentExtraAmmo 0)
        {
            
    reloading true;
            
    reloadAnimation.Play(reloadAnimationString);
            
    reloadSound.Play();
        }
        if (
    reloading  !reloadAnimation.IsPlaying(reloadAnimationString))
        {
            if (
    currentExtraAmmo >= clipSize currentClip)
            {
                
    currentExtraAmmo -= clipSize currentClip;
                
    currentClip clipSize;
            }
            if (
    currentExtraAmmo clipSize currentClip)
            {
                
    currentClip += currentExtraAmmo;
                
    currentExtraAmmo 0;
            }
            
    reloading false;
        }

        for(var 
    modelObject GameObject in gunModelObjects)
        {
            
    modelObject.layer 8;
        }
        
        
    rigidbody.useGravity false;
        
    outsideBox.GetComponent(Collider).enabled false;

        
    currentGunbobX Mathf.Sin(cameraObject.GetComponent(MouseLookScript).headbobStepCounter) * gunbobAmountX racioHipHold;
        
    currentGunbobY Mathf.Cos(cameraObject.GetComponent(MouseLookScript).headbobStepCounter 2) * gunbobAmountY * -racioHipHold;
        
        var 
    holdMuzzelFlash GameObject;
        var 
    holdSound GameObject;
        if (
    Input.GetButton("Fire1")  currentClip 0  !reloading)
        {
            if (
    waitTilNextFire <= 0)
            {
                
    currentClip -= 1;
                if (
    bullet)
                    
    Instantiate(bullet,bulletSpawn.transform.positionbulletSpawn.transform.rotation);
                if (
    bulletSound)
                    
    holdSound Instantiate(bulletSoundbulletSpawn.transform.positionbulletSpawn.transform.rotation);
                if (
    muzzelFlash)
                    
    holdMuzzelFlash Instantiate(muzzelFlashbulletSpawn.transform.positionbulletSpawn.transform.rotation);
                
    targetXRotation += (Random.value 0.5) * Mathf.Lerp(shootAngleRandomizationAimingshootAngleRandomizationNotAimingracioHipHold);
                
    targetYRotation += (Random.value 0.5) * Mathf.Lerp(shootAngleRandomizationAimingshootAngleRandomizationNotAimingracioHipHold);
                
    currentRecoilZPos -= recoilAmount;
                
    waitTilNextFire 1;
            }
        }
        
    waitTilNextFire -= Time.deltaTime fireSpeed;
     
        if (
    holdSound)
            
    holdSound.transform.parent transform;
        if (
    holdMuzzelFlash)
            
    holdMuzzelFlash.transform.parent transform;
     
        
    currentRecoilZPos Mathf.SmoothDampcurrentRecoilZPos0currentRecoilZPosVrecoilRecoverTime);

        
    cameraObject.GetComponent(MouseLookScript).currentTargetCameraAngle zoomAngle;
     
        if (
    Input.GetButton("Fire2")  !reloading){
            
    cameraObject.GetComponent(MouseLookScript).currentAimRacio aimRacio;
            
    racioHipHold Mathf.SmoothDamp(racioHipHold0racioHipHoldVhipToAimSpeed);}
        if (
    Input.GetButton("Fire2") == false || reloading){
            
    cameraObject.GetComponent(MouseLookScript).currentAimRacio 1;
            
    racioHipHold Mathf.SmoothDamp(racioHipHold1racioHipHoldVhipToAimSpeed);}
     
        
    transform.position cameraObject.transform.position + (Quaternion.Euler(0,targetYRotation,0) * Vector3(holdSide racioHipHold currentGunbobXholdHeight racioHipHold currentGunbobY0) + Quaternion.Euler(targetXRotationtargetYRotation0) * Vector3(0,0,currentRecoilZPos));
        
        
    targetXRotation Mathf.SmoothDamptargetXRotationcameraObject.GetComponent(MouseLookScript).xRotationtargetXRotationVrotateSpeed);
        
    targetYRotation Mathf.SmoothDamptargetYRotationcameraObject.GetComponent(MouseLookScript).yRotationtargetYRotationVrotateSpeed);
        
        
    transform.rotation Quaternion.Euler(targetXRotationtargetYRotation0);
    }
    if (!
    beingHeld)
    {
        for(var 
    modelObject GameObject in gunModelObjects)
        {
            
    modelObject.layer 0;
        }
        
        
    rigidbody.useGravity true;
        
    outsideBox.GetComponent(Collider).enabled true;
        
        
    countToThrow -= 1;
        if (
    countToThrow == 0)
            
    rigidbody.AddRelativeForce(0playerMovementScript.throwGunUpForceplayerMovementScript.throwGunForwardForce);    

        if (
    Vector3.Distance(transform.positionplayerTransform.position) < playerMovementScript.distToPickUpGun  Input.GetButtonDown("Use Key")  playerMovementScript.waitFrameForSwitchGuns <= 0)
        {
            
    playerMovementScript.currentGun.GetComponent(GunScript).beingHeld false;
            
    playerMovementScript.currentGun.GetComponent(GunScript).countToThrow 2;
            
    playerMovementScript.currentGun gameObject;
            
    beingHeld true;
            
    targetYRotation cameraObject.GetComponent(MouseLookScript).yRotation 180;
            
    playerMovementScript.waitFrameForSwitchGuns 2;
        }
    }
    }

    function 
    OnGUI ()
    {
        if(
    beingHeld)
        {
            for (var 
    int 1<= currentClipi++)
            {
                
    GUI.DrawTexture(Rect(ammoStartX + ((1) * (ammoSize.ammoSpacing)), ammoYammoSize.xammoSize.y), bulletHudTexture);
            }
            
    GUI.Label(ammoCountRectcurrentExtraAmmo.ToString());
            if (
    ammoDecorationTexture)
            
    GUI.DrawTextureammoDecorationHudRectammoDecorationTexture);
        }
    }
    MouseLookScript
    PHP:
    var defaultCameraAngle float 60;
    @
    HideInInspector
    var currentTargetCameraAngle float 60;
    @
    HideInInspector
    var racioZoom float 1;
    @
    HideInInspector
    var racioZoomV float;
    var 
    racioZoomSpeed float 0.2;

    var 
    lookSensitivity float 5;
    @
    HideInInspector
    var yRotation float;
    @
    HideInInspector
    var xRotation float;
    @
    HideInInspector
    var currentYRotation float;
    @
    HideInInspector
    var currentXRotation float;
    @
    HideInInspector
    var yRotationV float;
    @
    HideInInspector
    var xRotationV float;
    var 
    lookSmoothDamp float 0.1;
    @
    HideInInspector
    var currentAimRacio float 1;

    var 
    headbobSpeed float 1;
    @
    HideInInspector
    var headbobStepCounter float;
    var 
    headbobAmountX float 1;
    var 
    headbobAmountY float =1;
    @
    HideInInspector
    var parentLastPos Vector3;
    var 
    eyeHeightRacio float 0.9;

    function 
    Awake ()
    {
        
    parentLastPos transform.parent.position;
    }

    function 
    Update ()
    {
        if (
    transform.parent.GetComponent(PlayerMovementScript).grounded)
            
    headbobStepCounter += Vector3.Distance(parentLastPostransform.parent.position) * headbobSpeed;
        
    transform.localPosition.Mathf.Sin(headbobStepCounter) * headbobAmountX currentAimRacio;
        
    transform.localPosition.= (Mathf.Cos(headbobStepCounter 2) * headbobAmountY currentAimRacio) + (transform.localScale.eyeHeightRacio) - (transform.localScale.2);
        
        
    parentLastPos transform.parent.position;
        if (
    currentAimRacio == 1)
            
    racioZoom Mathf.SmoothDamp(racioZoom1racioZoomVracioZoomSpeed);
        else
            
    racioZoom Mathf.SmoothDamp(racioZoom0racioZoomVracioZoomSpeed);
        
        
    camera.fieldOfView Mathf.Lerp(currentTargetCameraAngledefaultCameraAngleracioZoom);
        
        
        
    yRotation += Input.GetAxis('Mouse X') * lookSensitivity currentAimRacio;
        
    xRotation -= Input.GetAxis('Mouse Y') * lookSensitivity currentAimRacio;
        
        
    xRotation Mathf.Clamp(xRotation, -9090);
        
        
    currentXRotation Mathf.SmoothDamp(currentXRotationxRotationxRotationVlookSmoothDamp);
        
    currentYRotation Mathf.SmoothDamp(currentYRotationyRotationyRotationVlookSmoothDamp);
        
        
    transform.rotation Quaternion.Euler(currentXRotationcurrentYRotation0);
    }
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Check if there are any colliders on your character that block the raycast. If that is the case, ignore them in your bullet script.
     
  5. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    there arent any colliders on my character
    ive tried changing bullet size bullet spawn position i moved gun i bit up and now i see the holes under my character but when i look straight [front of me] and shoot a bit down [45deegres down] the i cant see the bulletholes :(
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    It could be that there is a collider/trigger or something blocking the shooting for that height.

    Try that in your bullet script to isolate the issue:
    Code (csharp):
    1. if (Physics.Raycast(transform.position, transform.forward, hit, maxDist))
    2. {
    3.     if (decalHitWall  hit.transform.tag == "Level Parts") {
    4.         Instantiate(decalHitWall, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    5.     } else {
    6.         Debug.Log ("Hit something called " + hit.transform.name);
    7.     }
    8. } else {
    9.     Debug.Log ("Hit nothing with the bullet.");
    10. }
     
  7. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    my bullet script looks like that but i am getting error 'BCE0005: Unknown identifier: 'hit'.'

    Code (csharp):
    1. var maxDist : float = 1000000000;
    2. var decalHitWall : GameObject;
    3. var floatInFrontOfWall : float = 0.00001;
    4.  
    5. function Update ()
    6. {
    7.     if (Physics.Raycast(transform.position, transform.forward, hit, maxDist))
    8.     {
    9.         if (decalHitWall  hit.transform.tag == "Level Parts")
    10.             Instantiate(decalHitWall, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    11.     else
    12.         Debug.Log ('Hit something called ' + hit.transform.name);
    13.         }
    14.     else
    15.         Debug.Log ('Hit nothing with the bullet.');
    16.     Destroy(gameObject);
    17. }
     
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You have skipped the declaration of hit which exists in the original script.
     
  9. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    I did it wrote this in my bullet script fix my errors and i still getting that bug or something :(
    you can see on video i can shoot under me everywhere but when i shoot on the floor in angle around 45 degrees the bullet holes does not apear
     
    Last edited: Aug 25, 2013
  10. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    The changes I proposed you are only there to find the issue to help you eliminating the cause. Are you getting any message in the console?
     
  11. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    yes when i add those script you told me to do i am getting
    Hit nothing with the bullet.
    UnityEngine.Debug:Log(Object)
    BulletScript:Update() (at Assets/BulletScript.js:16)
    where ever i shoot in air or floor it says that
     
  12. SquidHorse

    SquidHorse

    Joined:
    May 26, 2013
    Posts:
    11
    Hey i3artyy,

    Know this is pretty old but if you're still having an issue, what you need to do is set the variable floatInFrontOfWall to something larger, such as 0.001, what I believe is happening is that your bullet decal (decalHitWall) is clipping through the objects it should be appearing on and not floating far enough in front of them. Let me know how that works out for you.
     
  13. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Hi man glad that you answer it :D i have still this problem and no one want to help me...
    I will check your solution and tell if it works
     
  14. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You may try to draw the ray, which may help you to find the issue.
    http://docs.unity3d.com/Documentation/ScriptReference/Debug.DrawRay.html

    If you see that the ray should hit the floor. Check once more the collider for that section.
     
  15. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Hmm still nothing ... maybe i gonna record video about these bugs
    http://www.youtube.com/watch?v=Gk-jqjDQdEA&feature=youtu.be
    here is the video but its without sound i dont know why its my first time using bandicam and i put full hd but on youtube is only 360p but anyway you can see that
    The bullet hole does not appear everytime
    Also i have a question how to make my gui of ammo and current ammo bigger
    And jumping how to make jumping more realistic

    Edit ok its HD now :D

    Thanks for helping!
     
    Last edited: Oct 12, 2013
  16. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Unfortunately the draw ray does not work in the way you have set it up. The point of it is that you see the ray along which you are shooting. Further you don't show, how you setup your colliders.
     
  17. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Colliders of what player, walls,gun ?
     
    Last edited: Oct 12, 2013
  18. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    All the objects that are not hit correctly. You have to think about it in an analytical way to make progress. What can go wrong? Either the raycast is not correct for some reason, so let's visualize it, or the colliders are wrong. They may not be there, may be ignored for some reason, ... .
    You have to work focused in that direction. You may only draw the ray when nothing is hit and keep if for a few seconds, such that you can see the actual issue. You can directly see the ray and check whether it should have hit a collider. If there should be a collider, have a look at the one and find out why it was not hit.
     
  19. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Ehhh i have no idea what to do and how to fix this bug... im just gonna leave away this file and start some different game ...