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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

I need help with my raycast shooting script

Discussion in 'Scripting' started by Legosdoctor, Nov 3, 2015.

  1. Legosdoctor

    Legosdoctor

    Joined:
    Jul 23, 2015
    Posts:
    21
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var Effect : Transform;
    4. var TheDamage = 100;
    5. function Update () {
    6.  
    7.     var hit : RaycastHit;
    8.     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
    9.    
    10.     if (Input.GetMouseButtonDown(0))
    11.     (
    12.         if (Physics.Raycast (ray, hit, 100))
    13.         (
    14.             var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
    15.             hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReciever);
    16.         )
    17.     )
    18.    
    19. }
    I get these errors:
    Assets/Standard Assets/Characters/FirstPersonCharacter/Prefabs/raycastshooting.js(13,17): BCE0043: Unexpected token: if.
    Assets/Standard Assets/Characters/FirstPersonCharacter/Prefabs/raycastshooting.js(13,20): BCE0044: expecting ), found '('.
    Assets/Standard Assets/Characters/FirstPersonCharacter/Prefabs/raycastshooting.js(13,21): UCE0001: ';' expected. Insert a semicolon at the end.
    Assets/Standard Assets/Characters/FirstPersonCharacter/Prefabs/raycastshooting.js(13,52): BCE0043: Unexpected token: ).
    Assets/Standard Assets/Characters/FirstPersonCharacter/Prefabs/raycastshooting.js(15,25): BCE0043: Unexpected token: var.
    Assets/Standard Assets/Characters/FirstPersonCharacter/Prefabs/raycastshooting.js(15,29): BCE0044: expecting ), found 'particleClone'.
    Assets/Standard Assets/Characters/FirstPersonCharacter/Prefabs/raycastshooting.js(16,116): BCE0043: Unexpected token: ;.
     
  2. flonch

    flonch

    Joined:
    Aug 20, 2014
    Posts:
    63
    Your syntax is wrong. You are using parenthesis, when you should be using curly braces, to enclose the lines within your if-statement.
    It should be like this.
    Code (JavaScript):
    1.  
    2. 10.    if (Input.GetMouseButtonDown(0))
    3. 11.    {
    4. 12.        if (Physics.Raycast (ray, hit, 100))
    5. 13.        {
    6. 14.            var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
    7. 15.            hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReciever);
    8. 16.        }
    9. 17.    }
    10.  
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    { } => scope block
    ( ) => mathematical parenthesis or function parameters


    also it would appear your shift key is stuck :p
     
  4. Legosdoctor

    Legosdoctor

    Joined:
    Jul 23, 2015
    Posts:
    21
    Thanks so much, i have one more question though, how do i get the effect to disappear in a set amount of time?
     
  5. kuschi85

    kuschi85

    Joined:
    Nov 1, 2015
    Posts:
    2
    You can use the "Clock" class and if the "duration" is higher than a threshold, delete your object or do what ever you like to do.
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    There's no Clock class in Unity.

    Destroy has an optional parameter for delay. Just use that.

    Code (csharp):
    1.  
    2. // remove effect after 3 seconds
    3. Destroy(particleClone.gameObject, 3);
    4.  
     
    flonch likes this.
  7. flonch

    flonch

    Joined:
    Aug 20, 2014
    Posts:
    63
    Although KelsoMRK's suggestion will get the job done, It would be best if you just created a pool of objects, and then recycle the objects when you are done with them opposed to instantiating and destroying every time you need a bullet.
    It might be a pain to set up the pooling system, but in the long run it would be very beneficial.
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Given that OP was having trouble with using curly brackets versus parenthesis I assumed pooling systems were probably a bit above their current skillset. Baby steps :)
     
  9. flonch

    flonch

    Joined:
    Aug 20, 2014
    Posts:
    63
    I understand where you were coming from, but I just wanted to point Legosdoctor in the right direction for the future.
     
  10. Legosdoctor

    Legosdoctor

    Joined:
    Jul 23, 2015
    Posts:
    21
    LOL, I was watching a video and the brackets looked like parenthesis since the text size was so small
     
  11. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It's an honest mistake when you're just beginning. I honestly can't stand video tutorials about programming.

    Good luck :)