Fps Gun help!

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

 var Projectile : Rigidbody;
var ProjectileSpeed : int = 10;
var FireRate : float = 10;  // The number of bullets fired per second
var lastfired : float;      // The value of Time.time at the last firing moment
function Update ()
{
     if (Input.GetButton("Fire1"))
     {
         if (Time.time - lastfired > 1 / FireRate)
         {
             lastfired = Time.time;
             var clone : Rigidbody;
             clone = Instantiate(Projectile, transform.position, transform.rotation);
             clone.velocity = transform.TransformDirection (Vector3.forward * ProjectileSpeed);
         }
     }
}

Thanks!

I don’t know Javascript real well, but if it were C#, here would be my suggestion

 Rigidbody Projectile;
int ProjectileSpeed = 10;
int remainingShots; //Shots remaining in clip
int maxClip; //Maximum clip size
float reloadRate; //How long it takes to swap clips;
float FireRate = 10;  // The number of bullets fired per second
float lastfired;      // The value of Time.time at the last firing moment
void Update ()
{
     if (Input.GetButton("Fire1") && remainingShots >0)
     {
         if (Time.time - lastfired > 1 / FireRate)
         {
             lastfired = Time.time;
             Rigidbody clone;
             clone = Instantiate(Projectile, transform.position, transform.rotation);
             clone.velocity = transform.TransformDirection (Vector3.forward * ProjectileSpeed);
            remainingShots -= 1;
         }
     }
     else if(Input.GetButton(key.R))
     {
           startCoroutine("reloadGun");
     }
}

IEnumerator reloadGun()
{
       yield return new WaitForSeconds(reloadRate);
       remainingShots = maxClip;
}

Something like that.

on line 15 you put var, i think this is a c# script

Indeed there was, and fixed. var could have worked, but it’s cleaner without it.

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

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 elseif(Input.GetButton(KeyCode.R) and that should fix both of these

Replace line 23 with StartCoroutine(reloadGun());

I’m trying to instantiate a prefab

In that case, replace all Rigidbody with GameObject

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

Are there lines associated with those errors? I don’t see where there would be an extra { or a namespace issue

using UnityEngine;
using System.Collections;

public class Gun : MonoBehaviour {

    GameObject Projectile;
    int ProjectileSpeed = 10;
    int remainingShots; //Shots remaining in clip
    int maxClip; //Maximum clip size
    float reloadRate; //How long it takes to swap clips;
    float FireRate = 10;  // The number of bullets fired per second
    float lastfired;      // The value of Time.time at the last firing moment
    void Update ()
    {
        if (Input.GetButton("Fire1") && remainingShots >0)
        {
            if (Time.time - lastfired > 1 / FireRate)
            {
                lastfired = Time.time;
                GameObject clone;
                clone = Instantiate(Projectile, transform.position, transform.rotation);
                clone.velocity = transform.TransformDirection (Vector3.forward * ProjectileSpeed);
                remainingShots -= 1;
            }
        }
        else if(Input.GetButton(KeyCode.R))
        {
            StartCoroutine(reloadGun());
        }
    }

      IEnumerator reloadGun ()
    {
        yield return new WaitForSeconds(reloadRate);
        remainingShots = maxClip;
    }
}

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’

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.

ok

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

using UnityEngine;
using System.Collections;

public class Gun : MonoBehaviour {

   GameObject Projectile;
   int ProjectileSpeed = 10;
   int remainingShots; //Shots remaining in clip
   int maxClip; //Maximum clip size
   float reloadRate; //How long it takes to swap clips;
   float FireRate = 10;  // The number of bullets fired per second
   float lastfired;      // The value of Time.time at the last firing moment
   void Update()
   {
       if (Input.GetButton("Fire1") && remainingShots >0)
       {
           if (Time.time - lastfired > 1 / FireRate)
           {
                lastfired = Time.time;
               GameObject clone;
                clone = Instantiate(Projectile, transform.position, transform.rotation) as GameObject;
                clone.velocity = transform.TransformDirection (Vector3.forward * ProjectileSpeed);
                remainingShots -= 1;
           }
       }
       else if(Input.GetKeyDown(KeyCode.R) && remainingShots != maxClip)
       {
           StartCoroutine(reloadGun());
       }
   }

      IEnumerator reloadGun ()
   {
        yield return new WaitForSeconds(reloadRate);
        remainingShots = maxClip;
   }
}

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?)

Oh, right. Yes. Velocity.
Replace line 23 with

clone.GetComponent<RigidBody>().velocity = transform.TransformDirection(Vector3.forward* ProjectileSpeed);

Error

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

If this is for JS it would be
clone.GetComponent(RigidBody)
instead of
clone.GetComponent()

Never mind i have a new script

 var prefabBullet:Transform;
var shootForce:float;
var shots : int = 0;
var maxShots : int = 8;
var shootSound : AudioClip;
var fireRate = 0.5;
private var nextFire = 0.0;
function Update()
{
     if(Input.GetButtonDown("Fire1") && shots < maxShots)
     {
         nextFire = Time.time + fireRate;
         var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
         instanceBullet.GetComponent.<Rigidbody>().AddForce(transform.forward * shootForce);
         GetComponent.<AudioSource>().PlayOneShot(shootSound);
         shots++;
     }
     else if (shots >= maxShots && Input.GetKeyDown(KeyCode.R))
     {
         shots = 0;
     }
}

But i also have a new problem that doesn’t have errors in it