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

Cannon shooting

Discussion in 'Scripting' started by Fressno, May 23, 2016.

  1. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    Hey!
    I have this script i want to use, if its possible.

    The first thing i want to ask if its possible to put a delay on when the cannon fired, depending on when the player presses the fire button.
    Like a charging time delay.
    second.
    Do i have to make another script if i want to have two different guns on one player model?
    for instance:
    I have a sci fi Tank that shoots a slow moving but deadly charged plasma shot that takes out an area of enemies.
    but there is a secondary laser minigun you can use.
    any pointers on how to make that?

    using UnityEngine;

    public class PlayerShooting : MonoBehaviour
    {
    public int damagePerShot = 20;
    public float timeBetweenBullets = 0.15f;
    public float range = 100f;


    float timer;
    Ray shootRay;
    RaycastHit shootHit;
    int shootableMask;
    ParticleSystem gunParticles;
    LineRenderer gunLine;
    AudioSource gunAudio;
    Light gunLight;
    float effectsDisplayTime = 0.2f;


    void Awake ()
    {
    shootableMask = LayerMask.GetMask ("Shootable");
    gunParticles = GetComponent<ParticleSystem> ();
    gunLine = GetComponent <LineRenderer> ();
    gunAudio = GetComponent<AudioSource> ();
    gunLight = GetComponent<Light> ();
    }


    void Update ()
    {
    timer += Time.deltaTime;

    if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
    {
    Shoot ();
    }

    if(timer >= timeBetweenBullets * effectsDisplayTime)
    {
    DisableEffects ();
    }
    }


    public void DisableEffects ()
    {
    gunLine.enabled = false;
    gunLight.enabled = false;
    }


    void Shoot ()
    {
    timer = 0f;

    gunAudio.Play ();

    gunLight.enabled = true;

    gunParticles.Stop ();
    gunParticles.Play ();

    gunLine.enabled = true;
    gunLine.SetPosition (0, transform.position);

    shootRay.origin = transform.position;
    shootRay.direction = transform.forward;

    if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
    {
    EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
    if(enemyHealth != null)
    {
    enemyHealth.TakeDamage (damagePerShot, shootHit.point);
    }
    gunLine.SetPosition (1, shootHit.point);
    }
    else
    {
    gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
    }
    }
    }
     
  2. CodingBruh

    CodingBruh

    Joined:
    May 11, 2015
    Posts:
    114
    You probably could push it all in one script. Also, how you delay, you create a timer variable in the script and every Update increment the value by 1 * time.deltaTime. Here's a little example:

    int timePassed = 0;

    void Update()
    {
    timePassed += 1 * time.deltaTime;
    if(timePassed > 3)
    {
    Debug.Log("I waited for 4 or more seconds!");
    }
    }

    Hope it helps! :)
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  4. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    Hey!
    I have this script i want to use, if its possible.

    The first thing i want to ask if its possible to put a delay on when the cannon fired, depending on when the player presses the fire button.
    Like a charging time delay.
    second.
    Do i have to make another script if i want to have two different guns on one player model?
    for instance:
    I have a sci fi Tank that shoots a slow moving but deadly charged plasma shot that takes out an area of enemies.
    but there is a secondary laser minigun you can use.
    any pointers on how to make that?

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class PlayerShooting : MonoBehaviour
    5. {
    6. public int damagePerShot = 20;
    7. public float timeBetweenBullets = 0.15f;
    8. public float range = 100f;
    9.  
    10.  
    11. float timer;
    12. Ray shootRay;
    13. RaycastHit shootHit;
    14. int shootableMask;
    15. ParticleSystem gunParticles;
    16. LineRenderer gunLine;
    17. AudioSource gunAudio;
    18. Light gunLight;
    19. float effectsDisplayTime = 0.2f;
    20.  
    21.  
    22. void Awake ()
    23. {
    24. shootableMask = LayerMask.GetMask ("Shootable");
    25. gunParticles = GetComponent<ParticleSystem> ();
    26. gunLine = GetComponent <LineRenderer> ();
    27. gunAudio = GetComponent<AudioSource> ();
    28. gunLight = GetComponent<Light> ();
    29. }
    30.  
    31.  
    32. void Update ()
    33. {
    34. timer += Time.deltaTime;
    35.  
    36. if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
    37. {
    38. Shoot ();
    39. }
    40.  
    41. if(timer >= timeBetweenBullets * effectsDisplayTime)
    42. {
    43. DisableEffects ();
    44. }
    45. }
    46.  
    47.  
    48. public void DisableEffects ()
    49. {
    50. gunLine.enabled = false;
    51. gunLight.enabled = false;
    52. }
    53.  
    54.  
    55. void Shoot ()
    56. {
    57. timer = 0f;
    58.  
    59. gunAudio.Play ();
    60.  
    61. gunLight.enabled = true;
    62.  
    63. gunParticles.Stop ();
    64. gunParticles.Play ();
    65.  
    66. gunLine.enabled = true;
    67. gunLine.SetPosition (0, transform.position);
    68.  
    69. shootRay.origin = transform.position;
    70. shootRay.direction = transform.forward;
    71.  
    72. if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
    73. {
    74. EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
    75. if(enemyHealth != null)
    76. {
    77. enemyHealth.TakeDamage (damagePerShot, shootHit.point);
    78. }
    79. gunLine.SetPosition (1, shootHit.point);
    80. }
    81. else
    82. {
    83. gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
    84. }
    85. }
    86. }
    87.  
     
  5. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    Look at your post. Does it look right to you?
     
  7. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    Think so.
    But now i need help in the actual problem.
    Can you guys help me?
     
  8. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    hey!
    i tried it, but i actually didnt know how to implement it into the existing code.
    all i get is errors. where should it be? and what should i take away?
     
  9. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    Im bumping this cus i still need help with it.
    I need help with this script to make a delay from when you press the fire button, and add a 2 second delay until the actuall shot if fired.
    Its suppose to simulate a power build up inside my cannon, like its sucking in power, creating an energy shot, then releasing it it.
    Please help?
     
  10. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    1.You can use WaitForSeconds in order to delay time. Try to work it into your code to get the delay your looking for.

    Scripting reference.
    https://docs.unity3d.com/ScriptReference/WaitForSeconds.html

    2. There's no rule whether you need to have one script or two scripts for 2 weapons. Although having separate scripts for each weapon is arguably the best. Create one script that can be used for a variety of weapon types, then duplicate it as many times as you need and then change the variables of each script to fit specific weapons. When switching weapons, keep the weapon scripts currently not in use disabled while keeping the selected weapon enabled.
     
  11. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    Im at a loss on what i should do?
    Should i make a reference on the WaitForSeconds coroutine script, or should i implement the WaitForSeconds code into the shooting code?
    if its the later, should i just make an:
    Code (CSharp):
    1.     public int damagePerShot = 20;
    2.     public float timeBetweenBullets = 0.15f;
    3.     public float range = 100f;
    4.     public float WaitForSeconds = 2f;
    and then add the WaitForSeconds code before the actual "Shoot" function? Like this:

    Code (CSharp):
    1.  void Update ()
    2.     {
    3.         timer += Time.deltaTime;
    4.  
    5.         if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
    6.         {
    7.             Shoot ();
    8.             WaitForSeconds ();
    9.         }
    10.  
    11.         if(timer >= timeBetweenBullets * effectsDisplayTime)
    12.         {
    13.             DisableEffects ();
    14.         }
    15.     }
    This should be easy, but its making my brain melt =) frustrating is a small word for such a little problem =)
     
  12. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Make shoot a coroutine then add WaitForSeconds() into it. Something like this. Specify how many seconds you want the pause to last in the brackets.

    Code (CSharp):
    1. void Update ()
    2. {
    3. timer += Time.deltaTime;
    4.  
    5. if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
    6. {
    7. StartCoroutine ("Shoot")
    8. }
    9.  
    10. if(timer >= timeBetweenBullets * effectsDisplayTime)
    11. {
    12. DisableEffects ();
    13. }
    14. }
    15.  
    16.  
    17. public IEnumerator Shoot ()
    18. {
    19. yield return new WaitForSeconds(5);
    20. timer = 0f;
    21.  
    22. //Play rest of code...
    23. }
    24.  
    25. }
    26.  
     
  13. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    Hey again =)
    thanx for the reply.
    Im trying to implement the code you gave, but its saying that i have a bracket in a wrong place.
    I cant see anything wrong with the brackets(curly braces). check it out =) :

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerShootingCannon : MonoBehaviour
    4. {
    5.     public int damagePerShot = 20;
    6.     public float timeBetweenBullets = 0.15f;
    7.     public float range = 100f;
    8.  
    9.  
    10.     float timer;
    11.     Ray shootRay;
    12.     RaycastHit shootHit;
    13.     int shootableMask;
    14.     ParticleSystem gunParticles;
    15.     LineRenderer gunLine;
    16.     AudioSource gunAudio;
    17.     Light gunLight;
    18.     float effectsDisplayTime = 0.2f;
    19.  
    20.  
    21.     void Awake ()
    22.     {
    23.         shootableMask = LayerMask.GetMask ("Shootable");
    24.         gunParticles = GetComponent<ParticleSystem> ();
    25.         gunLine = GetComponent <LineRenderer> ();
    26.         gunAudio = GetComponent<AudioSource> ();
    27.         gunLight = GetComponent<Light> ();
    28.     }
    29.  
    30.  
    31.     void Update ()
    32.     {
    33.         timer += Time.deltaTime;
    34.  
    35.         if(Input.GetButton ("Fire2") && timer >= timeBetweenBullets && Time.timeScale != 0)
    36.         {
    37.             StartCoroutine ("Shoot")
    38.         }
    39.  
    40.         if(timer >= timeBetweenBullets * effectsDisplayTime)
    41.         {
    42.             DisableEffects ();
    43.         }
    44.     }
    45.  
    46.  
    47.     public IEnumerator Shoot ()
    48.     {
    49.         yield return new WaitForSeconds(3);
    50.         timer = 0f;
    51.     }
    52.        
    53.  
    54.  
    55.     public void DisableEffects ()
    56.     {
    57.         gunLine.enabled = false;
    58.         gunLight.enabled = false;
    59.     }
    60.  
    61.  
    62.     void Shoot ()
    63.     {
    64.         timer = 0f;
    65.  
    66.         gunAudio.Play ();
    67.  
    68.         gunLight.enabled = true;
    69.  
    70.         gunParticles.Stop ();
    71.         gunParticles.Play ();
    72.  
    73.         gunLine.enabled = true;
    74.         gunLine.SetPosition (0, transform.position);
    75.  
    76.         shootRay.origin = transform.position;
    77.         shootRay.direction = transform.forward;
    78.  
    79.         if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
    80.         {
    81.             EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
    82.             if(enemyHealth != null)
    83.             {
    84.                 enemyHealth.TakeDamage (damagePerShot, shootHit.point);
    85.             }
    86.             gunLine.SetPosition (1, shootHit.point);
    87.         }
    88.         else
    89.         {
    90.             gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
    91.         }
    92.     }
    93. }
    94.  
    unity says this:
    Assets/Scripts/Player/PlayerShootingCannon.cs(38,17): error CS1525: Unexpected symbol `}'
     
  14. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    You forgot the ' ; ' after StartCoroutine("Shoot") on the 38th line
     
  15. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    thanx bud =)