- Home /
Reloading wont work
I have put this script together from various other scripts and it works ok until I run out of ammo and I reload. It dosent reload, I cant really say anything else it just dosent reload, can anyone help me?
var amountOfShots = 8;
var reloadTime = 1.5;
var refireRate : float = 0.1;
function ShotPoller()
{
while(true)
{
if(Input.GetButton("Fire1"))
{
Shoot();
yield WaitForSeconds(refireRate);
} else {
yield;
}
}
}
function Start()
{
StartCoroutine(ShotPoller());
}
if(Input.GetKeyDown("r")){
Reload();
}
function Reload (){
yield WaitForSeconds(reloadTime);
amountOfShots = 8;
}
var shotSound : AudioClip;
var bloodPrefab : Transform;
var sparksPrefab : Transform;
var hit : RaycastHit;
var range = 500;
function Shoot (){
if(amountOfShots > 0){
amountOfShots--;
if(shotSound){
audio.PlayOneShot(shotSound);
}
if (shotSound) audio.PlayOneShot(shotSound);
var hit: RaycastHit;
if (Physics.Raycast(transform.position, transform.forward, hit)){
var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
if (hit.transform.tag == "Enemy"){
if (bloodPrefab) Instantiate(bloodPrefab, hit.point, rot);
hit.transform.SendMessage("ApplyDamage", 20, SendMessageOptions.DontRequireReceiver);
} else {
if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
}
}
}
}
Answer by syclamoth · Dec 18, 2011 at 11:31 AM
You need an 'Update' somewhere in there. As it is, (because of Javascript weirdness, in a sane language this wouldn't even compile but that's a whole different rant), you are checking for the 'reload' button in 'Awake'- which executes exactly once, and only before the game even begins. You should wrap that into an 'Update' function-
function Update()
{
if(Input.GetKeyDown("r")){
Reload();
}
}
Or, better still- put it into your shoot poller!
while(true)
{
if(Input.GetButton("Fire1"))
{
Shoot();
}
if(Input.GetKeyDown("r")){
Reload();
}
yield;
}
Then, rejig shoot a little to put the 'WaitForSeconds' into that, and call 'Reload' if it's out of bullets!
function Shoot (){
if(amountOfShots > 0)
{
// do all of the shooting which already works
yield WaitForSeconds(refireRate);
} else {
Reload();
}
}
I have done this but now I have this problem that it shoots a ray every frame which I do not want can you help?
Did you remember to include the 'WaitForSeconds' in 'Shoot'? It's important. Unfortunately, they syntax for properly using Coroutines in Javascript is a little fuzzy- I'd be able to help you further if you were using C# and I could specify exactly what gets executed as a coroutine and what executes immediately just from the function declarations.
Ive got it working now I accidentally removed the yield wait for second like you said, thanks for all your help!
Well, you have to yield the two coroutines as well to make your loop wait for the subcoroutine to complete ;)
if(Input.GetButton("Fire1"))
{
yield StartCoroutine(Shoot());
}
if(Input.Get$$anonymous$$eyDown("r"))
{
yield StartCoroutine(Reload());
}
I'm not sure if you need StartCoroutine. I don't use UnityScript ;)
You don't need it. This is why I use C# for everything- it's a little bit confusing having the compiler just guess what you want it to do.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
A node in a childnode? 1 Answer
Stop animation when no more bullets 1 Answer
Unity sniper scope tutorial? 2 Answers
C# Throw GameObject Upon Mouse Click 1 Answer