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

Weird Raycast Shooting

Discussion in 'Scripting' started by doomlazy, Jul 28, 2014.

  1. doomlazy

    doomlazy

    Joined:
    Aug 24, 2013
    Posts:
    44
    So I have a script which is my modification of Brackeys Raycasting shooting script and all I want is for my raycast to do 20 damage to an enemy but even if I change the "TheDammage" variable at the top nothing happens. It still only does 10 damage to an enemy per hit. 10 clicks is way too much to kill someone so I ask of you, please help me get my enemies to die in 5 clicks. Also there is no error either its just not right.

    Info: Enemy has 100hp, locked damage is 10dmg, what I want is 20dmg (5 clicks of the mouse button)

    Heres my script:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var TheDammage = 20;
    4. var bulletHole : GameObject;
    5.  
    6. var bullets : int;
    7.  
    8. function start()
    9. {
    10.     bullets = 10;
    11. }
    12.  
    13. function Update()
    14. {
    15.  
    16.     Screen.showCursor = false;
    17.  
    18.     var fwd = transform.TransformDirection(Vector3.forward) * 150;
    19.     var hit : RaycastHit;
    20.     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
    21.  
    22.     if (Input.GetMouseButtonDown (0) && Physics.Raycast(transform.position, fwd, hit, 10))
    23.     {
    24.         if (hit.transform.tag == "Prop" && bullets > 0)
    25.         {
    26.              var bulletHoleClone = Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
    27.              Destroy(bulletHoleClone.gameObject, 10);
    28.         }
    29.     }
    30.    
    31.     if (Input.GetMouseButtonDown(0) && bullets > 0)
    32.     {  
    33.         bullets -= 1;
    34.            
    35.         if (Physics.Raycast (ray, hit, 100))
    36.         {
    37.             hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
    38.         }
    39.     }
    40.  
    41.     if (Input.GetKeyDown(KeyCode.R))
    42.     {
    43.         ReloadTheGun();
    44.     }
    45. }
    46.  
    47. function ReloadTheGun()
    48. {
    49.     yield WaitForSeconds (0.8);
    50.     bullets = 10;
    51. }
     
  2. Patrick234

    Patrick234

    Joined:
    Jun 25, 2014
    Posts:
    88
    Is the "ApplyDammage" method on the other script picking up " TheDammage " ?
     
  3. Patrick234

    Patrick234

    Joined:
    Jun 25, 2014
    Posts:
    88
    btw, i have no idea what locked damage is 10dmg means, but could it be a "damage cap" ?! in which case it negates the 20dmg as its capped at 10 dmg... ofcourse i dont know the rest of the code so i can only guess.
     
  4. doomlazy

    doomlazy

    Joined:
    Aug 24, 2013
    Posts:
    44
    By locked damage Im talking about the damage the script will only do even if I change the dammage variable

    Do you want to see the other script too ?

    Code (JavaScript):
    1.  
    2. var Health = 100;
    3.  
    4. function ApplyDammage (TheDammage : int)
    5. {
    6.     Health -= TheDammage;
    7.    
    8.     if(Health <= 0)
    9.     {
    10.         Dead();
    11.     }
    12. }
    13.  
    14. function Dead()
    15. {
    16.     Destroy (gameObject);
    17. }
     
  5. doomlazy

    doomlazy

    Joined:
    Aug 24, 2013
    Posts:
    44
    Please Someone Help ;(