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

Fps Gun help!

Discussion in 'Scripting' started by Shadowlash1221, Mar 8, 2016.

  1. Shadowlash1221

    Shadowlash1221

    Joined:
    Dec 30, 2015
    Posts:
    115
    So i have my gun and it shoots but i was wondering how i would add reload and ammo to my script, please help the script is below

    Code (JavaScript):
    1.  var Projectile : Rigidbody;
    2. var ProjectileSpeed : int = 10;
    3. var FireRate : float = 10;  // The number of bullets fired per second
    4. var lastfired : float;      // The value of Time.time at the last firing moment
    5. function Update ()
    6. {
    7.      if (Input.GetButton("Fire1"))
    8.      {
    9.          if (Time.time - lastfired > 1 / FireRate)
    10.          {
    11.              lastfired = Time.time;
    12.              var clone : Rigidbody;
    13.              clone = Instantiate(Projectile, transform.position, transform.rotation);
    14.              clone.velocity = transform.TransformDirection (Vector3.forward * ProjectileSpeed);
    15.          }
    16.      }
    17. }
    Thanks!
     
  2. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    I don't know Javascript real well, but if it were C#, here would be my suggestion

    Code (csharp):
    1.  Rigidbody Projectile;
    2. int ProjectileSpeed = 10;
    3. int remainingShots; //Shots remaining in clip
    4. int maxClip; //Maximum clip size
    5. float reloadRate; //How long it takes to swap clips;
    6. float FireRate = 10;  // The number of bullets fired per second
    7. float lastfired;      // The value of Time.time at the last firing moment
    8. void Update ()
    9. {
    10.      if (Input.GetButton("Fire1") && remainingShots >0)
    11.      {
    12.          if (Time.time - lastfired > 1 / FireRate)
    13.          {
    14.              lastfired = Time.time;
    15.              Rigidbody clone;
    16.              clone = Instantiate(Projectile, transform.position, transform.rotation);
    17.              clone.velocity = transform.TransformDirection (Vector3.forward * ProjectileSpeed);
    18.             remainingShots -= 1;
    19.          }
    20.      }
    21.      else if(Input.GetButton(key.R))
    22.      {
    23.            startCoroutine("reloadGun");
    24.      }
    25. }
    26.  
    27. IEnumerator reloadGun()
    28. {
    29.        yield return new WaitForSeconds(reloadRate);
    30.        remainingShots = maxClip;
    31. }
    Something like that.
     
    Last edited: Mar 8, 2016
  3. Shadowlash1221

    Shadowlash1221

    Joined:
    Dec 30, 2015
    Posts:
    115
    on line 15 you put var, i think this is a c# script
     
  4. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    Indeed there was, and fixed. var could have worked, but it's cleaner without it.
     
  5. Shadowlash1221

    Shadowlash1221

    Joined:
    Dec 30, 2015
    Posts:
    115
    I got 5 errors

    error CS0266: Cannot implicitly convert type `UnityEngine.Object' to `UnityEngine.Rigidbody'. An explicit conversion exists (are you missing a cast?)

    error CS0103: The name `key' does not exist in the current context

    error CS1502: The best overloaded method match for `UnityEngine.Input.GetButton(string)' has some invalid arguments

    error CS1503: Argument `#1' cannot convert `object' expression to type `string'

    error CS0103: The name `startCoroutine' does not exist in the current context
     
  6. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    I wrote that from work, so I'm really not surprised there's errors. I'm not a natural C# scripter, so I forget how some things are structured.
    What are you using? Is it a prefab? What are you trying to move? To me, it looks like you're trying to instantiate and move a component (Rigidbody) rather than a game object.

    Replace line 21 with
    Code (csharp):
    1. elseif(Input.GetButton(KeyCode.R)
    and that should fix both of these

    Replace line 23 with
    Code (csharp):
    1. StartCoroutine(reloadGun());
     
  7. Shadowlash1221

    Shadowlash1221

    Joined:
    Dec 30, 2015
    Posts:
    115
    I'm trying to instantiate a prefab
     
  8. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    In that case, replace all Rigidbody with GameObject
     
  9. Shadowlash1221

    Shadowlash1221

    Joined:
    Dec 30, 2015
    Posts:
    115
    So i replaced all rigidbody with gameobject but i got two errors

    error CS1525: Unexpected symbol `{'

    error CS0116: A namespace can only contain types and namespace declarations
     
  10. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    Are there lines associated with those errors? I don't see where there would be an extra { or a namespace issue
     
  11. Shadowlash1221

    Shadowlash1221

    Joined:
    Dec 30, 2015
    Posts:
    115
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Gun : MonoBehaviour {
    5.  
    6.     GameObject Projectile;
    7.     int ProjectileSpeed = 10;
    8.     int remainingShots; //Shots remaining in clip
    9.     int maxClip; //Maximum clip size
    10.     float reloadRate; //How long it takes to swap clips;
    11.     float FireRate = 10;  // The number of bullets fired per second
    12.     float lastfired;      // The value of Time.time at the last firing moment
    13.     void Update ()
    14.     {
    15.         if (Input.GetButton("Fire1") && remainingShots >0)
    16.         {
    17.             if (Time.time - lastfired > 1 / FireRate)
    18.             {
    19.                 lastfired = Time.time;
    20.                 GameObject clone;
    21.                 clone = Instantiate(Projectile, transform.position, transform.rotation);
    22.                 clone.velocity = transform.TransformDirection (Vector3.forward * ProjectileSpeed);
    23.                 remainingShots -= 1;
    24.             }
    25.         }
    26.         else if(Input.GetButton(KeyCode.R))
    27.         {
    28.             StartCoroutine(reloadGun());
    29.         }
    30.     }
    31.  
    32.       IEnumerator reloadGun ()
    33.     {
    34.         yield return new WaitForSeconds(reloadRate);
    35.         remainingShots = maxClip;
    36.     }
    37. }
    I tried to fix it but

    error CS0266: Cannot implicitly convert type `UnityEngine.Object' to `UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)

    error CS1061: Type `UnityEngine.GameObject' does not contain a definition for `velocity' and no extension method `velocity' of type `UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)

    error CS1502: The best overloaded method match for `UnityEngine.Input.GetButton(string)' has some invalid arguments

    error CS1503: Argument `#1' cannot convert `UnityEngine.KeyCode' expression to type `string'
     
  12. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    Hmm. Well, I won't be able to provide much more debugging until I get home and have my tools and am not putting off work.
     
  13. Shadowlash1221

    Shadowlash1221

    Joined:
    Dec 30, 2015
    Posts:
    115
  14. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    I had a little more time to kill. I think this will work, Shadow. I also put in a check to make sure players are not reloading when they already have a full clip
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Gun : MonoBehaviour {
    6.  
    7.    GameObject Projectile;
    8.    int ProjectileSpeed = 10;
    9.    int remainingShots; //Shots remaining in clip
    10.    int maxClip; //Maximum clip size
    11.    float reloadRate; //How long it takes to swap clips;
    12.    float FireRate = 10;  // The number of bullets fired per second
    13.    float lastfired;      // The value of Time.time at the last firing moment
    14.    void Update()
    15.    {
    16.        if (Input.GetButton("Fire1") && remainingShots >0)
    17.        {
    18.            if (Time.time - lastfired > 1 / FireRate)
    19.            {
    20.                 lastfired = Time.time;
    21.                GameObject clone;
    22.                 clone = Instantiate(Projectile, transform.position, transform.rotation) as GameObject;
    23.                 clone.velocity = transform.TransformDirection (Vector3.forward * ProjectileSpeed);
    24.                 remainingShots -= 1;
    25.            }
    26.        }
    27.        else if(Input.GetKeyDown(KeyCode.R) && remainingShots != maxClip)
    28.        {
    29.            StartCoroutine(reloadGun());
    30.        }
    31.    }
    32.  
    33.       IEnumerator reloadGun ()
    34.    {
    35.         yield return new WaitForSeconds(reloadRate);
    36.         remainingShots = maxClip;
    37.    }
    38. }
     
  15. Shadowlash1221

    Shadowlash1221

    Joined:
    Dec 30, 2015
    Posts:
    115
    Theres one error

    error CS1061: Type `UnityEngine.GameObject' does not contain a definition for `velocity' and no extension method `velocity' of type `UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
     
  16. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    Oh, right. Yes. Velocity.
    Replace line 23 with
    Code (csharp):
    1. clone.GetComponent<RigidBody>().velocity = transform.TransformDirection(Vector3.forward* ProjectileSpeed);
     
  17. Shadowlash1221

    Shadowlash1221

    Joined:
    Dec 30, 2015
    Posts:
    115
    Error

    error CS0246: The type or namespace name `RigidBody' could not be found. Are you missing a using directive or an assembly reference?
     
  18. Necromunger

    Necromunger

    Joined:
    Mar 19, 2013
    Posts:
    3
    If this is for JS it would be
    clone.GetComponent(RigidBody)
    instead of
    clone.GetComponent<RigidBody>()
     
  19. Shadowlash1221

    Shadowlash1221

    Joined:
    Dec 30, 2015
    Posts:
    115
    Never mind i have a new script

    Code (JavaScript):
    1.  var prefabBullet:Transform;
    2. var shootForce:float;
    3. var shots : int = 0;
    4. var maxShots : int = 8;
    5. var shootSound : AudioClip;
    6. var fireRate = 0.5;
    7. private var nextFire = 0.0;
    8. function Update()
    9. {
    10.      if(Input.GetButtonDown("Fire1") && shots < maxShots)
    11.      {
    12.          nextFire = Time.time + fireRate;
    13.          var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
    14.          instanceBullet.GetComponent.<Rigidbody>().AddForce(transform.forward * shootForce);
    15.          GetComponent.<AudioSource>().PlayOneShot(shootSound);
    16.          shots++;
    17.      }
    18.      else if (shots >= maxShots && Input.GetKeyDown(KeyCode.R))
    19.      {
    20.          shots = 0;
    21.      }
    22. }
    But i also have a new problem that doesn't have errors in it