Search Unity

Localscale on Raycast

Discussion in 'Scripting' started by Aces_DG02, Apr 21, 2015.

  1. Aces_DG02

    Aces_DG02

    Joined:
    Apr 21, 2015
    Posts:
    2
    Hello everybody,

    I'm new here and have a problem I can't solve on my own.
    I'm making a maze first person game where the player have the opportunity to place boms and shoot at obstacles.
    The shooting in the game works fine but the bom placing not.
    The problem is that when a bom is placed it need to shoot a raycast in all directions (front, back, right en left) en when a ray hits i doesn't scale in that direction and when the ray doesn't hit it scales in that direction.

    I have a empty game object called "BomSpawner" on location 0,0,0 for the bom spawning.
    I also have a empty game object called "BomPlace" attached to the player for the location of the bom.
    For the explosion I use a cube that spawns at the location of the bom.

    This is the script I use

    Code (CSharp):
    1.  
    2.  
    3. usingUnityEngine;
    4. usingSystem.Collections;
    5.  
    6. publicclassBomScript : MonoBehaviour
    7. {
    8. //Locatievandeplayerofenemybom
    9. publicTransformplayerBom;
    10. publicTransformenemyBom;
    11.  
    12.  
    13. //Bomvariables
    14. publicfloatbomTimer;
    15. publicfloatexplosieTimer;
    16. publicGameObjectexplosie;
    17. publicGameObjectbomPrefab;
    18.  
    19. //Bomraycastsvariables
    20. publicfloatrange;
    21. publicfloatmaxDistance;
    22.  
    23. //Usethisforinitialization
    24. voidStart ()
    25. {
    26.  
    27. }
    28.  
    29. //Updateiscalledonceperframe
    30. voidUpdate ()
    31. {
    32. RaycastHitenemyHit;
    33. RaycastHithit1;
    34. RaycastHithit2;
    35. RaycastHithit3;
    36. RaycastHithit4;
    37.  
    38. Rayfront = newRay (playerBom.position, Vector3.forward);
    39. Rayback = newRay (transform.position, Vector3.back);
    40. Rayright = newRay (transform.position, Vector3.right);
    41. Rayleft = newRay (transform.position, Vector3.left);
    42.  
    43. if (Input.GetMouseButtonDown (0))
    44. {
    45. //Playerbom
    46. GameObjectbom = Instantiate (bomPrefab, playerBom.position, playerBom.rotation) asGameObject;
    47. GameObjectexp = Instantiate (explosie, playerBom.position, Quaternion.identity) asGameObject;
    48.  
    49. StartCoroutine(DestroyBom());
    50. StartCoroutine(DestroyExplosie());
    51.  
    52.  
    53. /*RayCastbasis: Physics.Raycast (vector3origin, vector3direction, raycasthit,
    54. hitinfo, floatdistance, intlayermask */
    55. //http://unity3d.com/learn/tutorials/modules/beginner/physics/raycasting
    56.  
    57. if(Physics.Raycast (playerBom.position, Vector3.forward, outhit1, range))
    58. {
    59. GameObject.FindGameObjectWithTag("Explosie").transform.localScale += newVector3 (0.1f, 0, 0);
    60. }
    61.  
    62. if(Physics.Raycast (playerBom.position, Vector3.back, outhit2, range))
    63. {
    64. GameObject.FindGameObjectWithTag("Explosie").transform.localScale += newVector3 (-0.1f, 0, 0);
    65. }
    66.  
    67. if(Physics.Raycast (playerBom.position, Vector3.right, outhit3, range))
    68. {
    69. GameObject.FindGameObjectWithTag("Explosie").transform.localScale += newVector3 (0, 0.1f, 0);
    70. }
    71.  
    72. if(Physics.Raycast(playerBom.position, Vector3.left, outhit4, range))
    73. {
    74. GameObject.FindGameObjectWithTag("Explosie").transform.localScale += newVector3 (0, -0.1f, 0);
    75. }
    76. }
    77. /*
    78. //Enemyraycastalsplayerrangeindebuurtkomt
    79. if (Physics.Raycast (transform.position + (newVector3 (0, 1.0f, 0)), transform.forward, outenemyHit, range))
    80. {
    81. Instantiate (bomPrefab, enemyBom.position, enemyBom.rotation);
    82. Instantiate (explosie, enemyBom.position, Quaternion.identity);
    83. StartCoroutine (DestroyBom ());
    84. Debug.Log("hit");
    85. }
    86. */
    87.  
    88. }
    89.  
    90. IEnumeratorDestroyBom()
    91. {
    92. yieldreturnnewWaitForSeconds (bomTimer);
    93. Destroy (GameObject.FindWithTag("Clone"));
    94. }
    95.  
    96. IEnumeratorDestroyExplosie()
    97. {
    98. yieldreturnnewWaitForSeconds (explosieTimer);
    99. Destroy (GameObject.FindGameObjectWithTag ("Explosie"));
    100. }
    101.  
    102.  
    103. }
    104.  
    PS: if my english is bad, excuse me
     
    Last edited: Apr 21, 2015