- Home /
musket gun script is not working
this script is supposed to make the gun shoot once and then reload but it just lets me keep shooting i am pretty much a beginner with scripting
here is the script
var reloadTime = 5;
var totalAmmo = 10;
var ammoCount = 1;
function Update () {
if (ammoCount >= 1) {
CanShoot = true;
}
if (ammoCount <= 0) {
CanShoot = false;
}
if (CanShoot == false) {
reload();
}
if (CanShoot == true) {
Shoot();
}
}
function Shoot () {
var hit : RaycastHit;
var ray: Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast (ray, hit, 100))
{
hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
{ammoCount -= 1;
}
}
}
}
function reload () {
if (Input.GetKeyDown("r")) {
yield WaitForSeconds(reloadTime); //waits for "reloadTime" before adding ammo
ammoCount += 1; //adds ammo to our "clip" based off the reloadAmount
totalAmmo -= 1; //subtracts whatever the reloadAmount was from our total ammo every time we reload
}
}
Try setting canShoot to false after your shot, maybe its not setting it, also when you declare it have it set to false
Answer by froYo · Aug 30, 2014 at 09:56 PM
It looks to me like you are only reducing the ammo count if your raycast is detecting a hit, whereas it should subtract ammo regardless of a hit.
function Update ()
{
if (ammoCount >= 1)
{
CanShoot = true;
}
if (ammoCount <= 0)
{
CanShoot = false;
}
if (CanShoot == false)
{
reload();
}
if (CanShoot == true)
{
Shoot();
}
}
function Shoot ()
{
var hit : RaycastHit;
var ray: Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast (ray, hit, 100))
{
hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
}
ammoCount -= 1;
}
}
function reload ()
{
if (Input.GetKeyDown("r"))
{
yield WaitForSeconds(reloadTime); //waits for "reloadTime" before adding ammo
ammoCount += 1; //adds ammo to our "clip" based off the reloadAmount
totalAmmo -= 1; //subtracts whatever the reloadAmount was from our total ammo every time we reload
}
}
Answer by BMayne · Aug 30, 2014 at 11:51 PM
Hey There,
You can make your code a lot simpler by removing that can fire checks that you are doing. I have made a quick example of a simple firing script. It is written in C# so you will have to convert it (no copying and pasting :) ) it should not take to long.
float currentAmmo = 5.0f;
float currentReloadTime = 0.0f;
bool isReloading = false;
const float RELOAD_TIME = 2.0f;
const int MAX_AMMO = 8;
void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
if (currentAmmo != MAX_AMMO && !isReloading) // if we already have max ammo lets leave or if we are already reloading
{
isReloading = true; // We are now reloading
currentReloadTime = RELOAD_TIME; //Set our current load time
}
}
if (isReloading)
ReloadStep();
else
GunStep();
}
void GunStep()
{
if (currentAmmo <= 0)
return; //No ammo so we leave.
if (!Input.GetMouseButtonDown(0)) //the '!' means Not. so the mouse button is Not Down
return; //We are not firing so we don't need to be here
//We fired our gun so we need to use a bullet
currentAmmo--;
//Start our making our raycast
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(new Vector3((float)Screen.width * 0.5f, (float)Screen.height * 0.5f, 0.0f));
//Fire the ray and tell anything we hit to apply damage
if (Physics.Raycast(ray, out hit, 100))
{
hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
}
}
void ReloadStep()
{
currentReloadTime -= Time.deltaTime; //We count down
if( currentReloadTime <= 0.0f )
{
isReloading = false;
currentAmmo = MAX_AMMO;
}
}
As you can see you don't need much more then that. Let me know if you have any questions.
Regards,
Edited: Added reloading
Boom reloading!
Also as I posted this I noticed I put constants you don't really have to worry about them if you don't know what they are. They are just values that can't be altered by code.
um i am having trouble fully understanding this is it possible you could post this in unityscript
I am not the best with UnityScript, C# developer here :(
What part are you stuck on?
Your answer
Follow this Question
Related Questions
ammo display javascript 1 Answer
Need coding help with fps 0 Answers
How do I make a gun? 1 Answer
How to send a raycast to an object to give me ammo? 0 Answers
need help converting gun script from c# to javascript 1 Answer