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

Raycast problem [Sometimes it doesnt detect]

Discussion in 'Editor & General Support' started by i3artyy2222, May 6, 2014.

  1. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Hello all,
    Ive been making my own game but i have some problem, i think is something to do with raycast or something.
    When i shoot sometimes the bullet holes appear but sometime it doesnt, like it doesnt detect sometimes i dont know here is the video.

    https://www.youtube.com/watch?v=K76lv0oBL8U&feature=youtu.be

    Thanks For helping !!!
     
  2. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Can any one help me :( ?
     
  3. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    Code?

    Maybe your raycast is too short?
     
  4. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Here is my raycast script
    Code (csharp):
    1. var maxDist : float = 100000000;
    2. var floatInFrontOfWall : float = 0.00001;
    3.  
    4. var ConcreteHole : GameObject[];
    5. var WoodHole : GameObject[];
    6. var MetalHole : GameObject[];
    7. var DirtHole : GameObject[];
    8.  
    9. var DirtParticle : Transform;
    10. var MetalParticle : Transform;
    11. var ConcreteParticle : Transform;
    12. var WaterParticle : Transform;
    13. var WoodParticle : Transform;
    14. var UntaggedParticle : Transform;
    15.  
    16. var power : int = 50;
    17.  
    18.  
    19. function Update ()
    20. {
    21.     var hit : RaycastHit;
    22.     var fwd = transform.TransformDirection(Vector3.forward);
    23.     if (Physics.Raycast(transform.position, transform.forward, hit, maxDist))
    24.     {
    25.         if (ConcreteHole  hit.transform.tag == "Concrete"){
    26.             Instantiate(ConcreteParticle, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    27.             Instantiate(ConcreteHole[Random.Range(0,3)], hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    28.     }
    29.         if (WoodHole  hit.transform.tag == "Wood"){
    30.             Instantiate(WoodParticle, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    31.             Instantiate(WoodHole[Random.Range(0,2)], hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    32.             }
    33.         if (MetalHole  hit.transform.tag == "Metal"){
    34.             Instantiate(MetalParticle, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    35.             Instantiate(MetalHole[Random.Range(0,3)], hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    36.             }
    37.         if (DirtHole  hit.transform.tag == "Dirt"){
    38.             Instantiate(DirtParticle, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    39.             }
    40.         if (hit.transform.tag == "Untagged"){
    41.             Instantiate(UntaggedParticle, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    42.             }
    43.     Destroy(gameObject);
    44.         if (hit.rigidbody !=null)
    45.             hit.rigidbody.AddForceAtPosition(fwd * power, hit.point);
    46. }
    47. }
     
  5. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Can anyone help me ?
    And is it better to have 2 different scripts [For gun and bullet] bullet is basically raycast script and gun is about ammo shooting and stuff or is it better to join them together ?
     
  6. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Help anyone ? please guys i checked whole google and could find similar problem can you help me :?
    Im asking for help on unity 3d forum, so why no one is helping
     
  7. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,937
  8. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Yes i have noticed that sometimes [When it doesnt detect] i saw on scene view that it goes sometimes through the object [That doesnt detect] and hit something behind.
     
  9. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,937
    Also how does the shooting happens?

    As you are constantly raycasting inside Update loop,
    but also destroying the same gameobject there..(with code after the destroy..)

    Maybe you should use function Start() instead Update() ?

    What kind of colliders the wall has?
     
  10. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    It has mesh colliders but it happens to any object even if its box collider or sphere

    Edit i have changed from update to start its same problem :( nothing has changed

    Edit i have also changed the distance to 1000 still same problem :(

    I have also a gun script maybe it will help :(

    Code (csharp):
    1. var beingHeld : boolean = false;
    2. var outsideBox : GameObject;
    3. @HideInInspector
    4. var countToThrow : int = -1;
    5. @HideInInspector
    6. var playerTransform : Transform;
    7. @HideInInspector
    8. var playerMovementScript : PlayerMovementScript;
    9.  
    10.  
    11. var cameraObject : GameObject;
    12. @HideInInspector
    13. var targetXRotation : float;
    14. @HideInInspector
    15. var targetYRotation : float;
    16. @HideInInspector
    17. var targetXRotationV : float;
    18. @HideInInspector
    19. var targetYRotationV : float;
    20.  
    21. var isSingleShot : boolean = false;
    22. var isAutomatic : boolean = false;
    23.  
    24. var rotateSpeed : float = 0.3;
    25.  
    26. var holdHeight : float = -0.5;
    27. var holdSide : float = 0.5;
    28. var racioHipHold : float = 1;
    29. var hipToAimSpeed : float = 0.1;
    30. @HideInInspector
    31. var racioHipHoldV : float;
    32. var aimRacio : float = 0.4;
    33. var zoomAngle : float = 30;
    34.  
    35.  
    36. var ShootSpeed : float = 0.1; //time between each shot. Lower the number, faster the burst
    37. private var waitTilNextFire : float = 0;
    38. private var ShootCooler : float = 0.3;
    39. private var shotTime : float = 0; //Must always remain at 0
    40. private var NextShot : float;
    41.  
    42.  
    43.  
    44. var bullet : GameObject;
    45. var bulletSpawn : GameObject;
    46.  
    47. var shootAngleRandomizationAiming : float = 5;
    48. var shootAngleRandomizationNotAiming : float = 15;
    49.  
    50. var recoilAmount : float = 0.5;
    51. var recoilRecoverTime : float = 0.2;
    52. @HideInInspector
    53. var currentRecoilZPos : float;
    54. @HideInInspector
    55. var currentRecoilZPosV : float;
    56.  
    57. var gunSound : GameObject;
    58.  
    59. var gunModelObjects : GameObject[];
    60.  
    61. var reloading : boolean = false;
    62. var reloadAnimation : Animation;
    63. var reloadSound : AudioSource;
    64. var reloadAnimationString : String;
    65.  
    66. var MagSize : int = 25;
    67. var currentMag : int = 25;
    68. var maxExtraAmmo : int = 100;
    69. var currentExtraAmmo : int = 100;
    70. var AmmoHUD : GUIStyle;
    71.  
    72. function Start()
    73. {
    74.         NextShot = ShootSpeed;
    75. }
    76.  
    77. function Awake()
    78. {
    79.     playerTransform = GameObject.FindWithTag("Player").transform;
    80.     playerMovementScript = GameObject.FindWithTag("Player").GetComponent(PlayerMovementScript);
    81.     countToThrow = -1;
    82. }
    83.  
    84. function LateUpdate ()
    85. {
    86. if (currentMag > MagSize)
    87.     currentMag = MagSize;
    88. if (currentExtraAmmo > maxExtraAmmo)
    89.     currentExtraAmmo = maxExtraAmmo;
    90. if (currentMag < 0)
    91.     currentMag = 0;
    92. if (currentExtraAmmo < 0)
    93.     currentExtraAmmo = 0;
    94.    
    95. if(waitTilNextFire > 0)
    96. {
    97.     waitTilNextFire -= Time.deltaTime;
    98. }
    99. if(waitTilNextFire > 0)
    100. {
    101.     waitTilNextFire = 0;
    102. }
    103.    
    104. if (beingHeld)
    105. {
    106.     if (!reloading  Input.GetButtonDown("Reload")  currentMag < MagSize  currentExtraAmmo > 0)
    107.     {
    108.         reloading = true;
    109.         reloadAnimation.Play(reloadAnimationString);
    110.         reloadSound.Play();
    111.     }
    112.     if (!reloading  Input.GetButtonDown("Fire1")  currentMag == 0  currentExtraAmmo > 0)
    113.     {
    114.         reloading = true;
    115.         reloadAnimation.Play(reloadAnimationString);
    116.         reloadSound.Play();
    117.     }
    118.     if (reloading  !reloadAnimation.IsPlaying(reloadAnimationString))
    119.     {
    120.         if (currentExtraAmmo >= MagSize - currentMag)
    121.         {
    122.             currentExtraAmmo -= MagSize - currentMag;
    123.             currentMag = MagSize;
    124.         }
    125.         if (currentExtraAmmo < MagSize - currentMag)
    126.         {
    127.             currentMag += currentExtraAmmo;
    128.             currentExtraAmmo = 0;
    129.         }
    130.         reloading = false;
    131.     }
    132.     for(var modelObject : GameObject in gunModelObjects)
    133.     {
    134.         modelObject.layer = 8;
    135.     }
    136.    
    137.     rigidbody.useGravity = false;
    138.     outsideBox.GetComponent(Collider).enabled = false;
    139.     var holdSound : GameObject;
    140.     if (Input.GetButtonDown("Fire1")  currentMag > 0  !reloading  isSingleShot)
    141.     {
    142.         if (waitTilNextFire == 0)
    143.         {
    144.             currentMag -= 1;
    145.             if (bullet)
    146.                 Instantiate(bullet,bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    147.             if (gunSound)
    148.                 holdSound = Instantiate(gunSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    149.             targetXRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, racioHipHold);
    150.             targetYRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, racioHipHold);
    151.             currentRecoilZPos -= recoilAmount;
    152.             waitTilNextFire = ShootCooler;
    153.         }
    154.     }
    155.    
    156.     if (Input.GetButton("Fire1")  currentMag > 0  !reloading  isAutomatic)
    157.     {
    158.         shotTime += Time.deltaTime;
    159.         if(shotTime >= NextShot)
    160.         {
    161.             currentMag -= 1;
    162.             if (bullet)
    163.                 Instantiate(bullet,bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    164.             if (gunSound)
    165.                 holdSound = Instantiate(gunSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    166.             targetXRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, racioHipHold);
    167.             targetYRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, racioHipHold);
    168.             currentRecoilZPos -= recoilAmount;
    169.             NextShot += ShootSpeed;
    170.         }
    171.     }
    172.    
    173.     if (holdSound)
    174.         holdSound.transform.parent = transform;
    175.    
    176.     currentRecoilZPos = Mathf.SmoothDamp( currentRecoilZPos, 0, currentRecoilZPosV, recoilRecoverTime);
    177.    
    178.     cameraObject.GetComponent(MouseLookScript).currentTargetCameraAngle = zoomAngle;
    179.  
    180.     if (Input.GetButton("Fire2")  !reloading){
    181.         cameraObject.GetComponent(MouseLookScript).currentAimRacio = aimRacio;
    182.         racioHipHold = Mathf.SmoothDamp(racioHipHold, 0, racioHipHoldV, hipToAimSpeed);}
    183.        
    184.     if (Input.GetButton("Fire2") == false || reloading){
    185.         cameraObject.GetComponent(MouseLookScript).currentAimRacio = 1;
    186.         racioHipHold = Mathf.SmoothDamp(racioHipHold, 1, racioHipHoldV, hipToAimSpeed);}
    187.         transform.position = cameraObject.transform.position + (Quaternion.Euler(0,targetYRotation,0) * Vector3(holdSide * racioHipHold, holdHeight * racioHipHold, 0) + Quaternion.Euler(targetXRotation, targetYRotation, 0) * Vector3(0,0,currentRecoilZPos));
    188.        
    189.         targetXRotation = Mathf.SmoothDamp( targetXRotation, cameraObject.GetComponent(MouseLookScript).xRotation, targetXRotationV, rotateSpeed);
    190.         targetYRotation = Mathf.SmoothDamp( targetYRotation, cameraObject.GetComponent(MouseLookScript).yRotation, targetYRotationV, rotateSpeed);
    191.        
    192.         transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
    193.        
    194.         }
    195. if (!beingHeld)
    196. {
    197.     for(var modelObject : GameObject in gunModelObjects)
    198.     {
    199.         modelObject.layer = 0;
    200.     }
    201.    
    202.     rigidbody.useGravity = true;
    203.     outsideBox.GetComponent(Collider).enabled = true;
    204.  
    205.     countToThrow -= 1;
    206.     if (countToThrow ==0)
    207.         rigidbody.AddRelativeForce(0, playerMovementScript.throwGunUpForce, playerMovementScript.throwGunForwardForce);
    208.        
    209.     if (Vector3.Distance(transform.position, playerTransform.position) < playerMovementScript.distToPickUpGun  Input.GetButtonDown("Use Key")  playerMovementScript.waitFrameForSwitchGuns <= 0)
    210.     {
    211.         playerMovementScript.currentGun.GetComponent(GunScript).beingHeld = false;
    212.         playerMovementScript.currentGun.GetComponent(GunScript).countToThrow = 2;
    213.         playerMovementScript.currentGun = gameObject;
    214.         beingHeld = true;
    215.         targetYRotation = cameraObject.GetComponent(MouseLookScript).yRotation - 180;
    216.         playerMovementScript.waitFrameForSwitchGuns = 2;
    217.     }
    218.    
    219. }
    220. }
    221.  
     
    Last edited: May 8, 2014
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Have you tried what he suggested with Debug.DrawLine and Debug.Log-ing what it hits each frame?
     
  12. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Yes i have tried that, sometimes when i shoot and when it doesnt detect it goes through the object, when i shoot at the wall it shows in console that i have hit ' WALL,WALL,WALL,WALL,PLANE,WALL,WALL,PLACE,TERRAIN' i dont have no idea how to fix it :(
     
  13. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
  14. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,937
    show screenshot with Debug.Drawline, does it really go through the collider?

    Maybe the bullet has rigidbody and it collides something after instantiated (and turns to wrong direction)?
     
  15. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    But when i add this
    Debug.Log (hit.transform.gameObject.name);// it should return the obstacle
    Debug.DrawLine (transform.parent.position, hit.point, Color.red);

    to my raycast script the red line is showing but its like at the edge of terrain

    edit: I have checked the bullet prefab doesnt have rigidbody
    $Untitled.png
     
    Last edited: May 9, 2014
  16. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
  17. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    So no one know how to fix it :( ?
     
  18. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
  19. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,937
    You have to try different things.. or attach sample unitypackage for testing.

    If your debug.drawline is sometimes starting from 0,0,0, try to find out why..
    (and draw the debug.line exactly same way as you do raycast, not from parent..)

    DontGoThroughThings:
    You dont need that if your bullets dont move, do they?

    Are the bullets parented at anytime to some object?
     
  20. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    bullet is an empty game object with the script
     
  21. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    It seems like i fix it :D by my own i think the bullet spawn was colliding with gun model so i moved it further a bit and it works :D
    also i change some of my script but cant remember what did i change :D