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);
}
}
}
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;
}
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
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’
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;
}
}
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?)