- Home /
need help converting gun script from c# to javascript
a user named BMayne answered my question here
he gave this script as a answer to show me a better way to do that kind of shooting
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;
}
}
and I use javascript so i was wondering if someone could convert this to javascript so i can understand it better. thanks in advance.
Answer by kacyesp · Sep 02, 2014 at 10:50 PM
This might help: http://answers.unity3d.com/questions/497764/convert-c-to-js-1.html
If you read through the answer and comments, you'll find a bunch of references for how to do this yourself.
thank you so much, this is exactly what i was looking for, now i can convert it myself!!!
@goten000023 Feel free to accept the answer and possibly upvote it :)
Your answer
Follow this Question
Related Questions
having problems with my raycast shooting script 0 Answers
im having trouble with raycast shooting script 0 Answers
musket gun script is not working 3 Answers
Third Person Shooting Script Stopped Working 1 Answer
raycast to object, load wrong script!? 2 Answers