- Home /
Question by
PWBotha · Apr 16, 2013 at 01:17 AM ·
c#coroutinewaitforseconds
Doing WaitForSeconds in C#
I've been trying to get WaitForSeconds to work, the code still ends up executing near instantly. Here's the first variant:
using UnityEngine;
using System.Collections;
public class TWW :MonoBehaviour
{
public AudioClip soundFire;
public GameObject animObject;
private float lastShot = -10.0F;
void Update(){
ShootStuff();
}
int ShootStuff()
{
/*animObject.animation["Shot"].speed = 3;
animObject.animation.Play("Shot");
audio.clip = soundFire;
audio.Play();
lastShot = Time.time;*/
print ("first");
StartCoroutine(WaitTime(5)); // CALL
print ("second");
/*animObject.animation["Shot"].speed = -3;
animObject.animation.Play("Shot");*/
return 1;
}
IEnumerator WaitTime(int arg1){yield return new WaitForSeconds(arg1);}
}
////////////////////////// I cannot get any feedback animations in ShootStuff()
using UnityEngine;
using System.Collections;
public class GunScript :MonoBehaviour
{
public AudioClip pickupAmmo;
public AudioClip soundFire;
public Camera mainCamera;
public Color colour;
public Font font;
public GameObject animObject;
public GameObject ammoPrefab;
public GameObject launchPosition;
public GameObject muzzleObject;
public float fovMin = 45.0F;
public float fovMax = 90.0F;
public float fovSpeed = 0.25F;
public float reloadTime = 0.5F;
public float waitTime = 0.1F;
public int ammoCount = 15;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetMouseButton(1))
{
mainCamera.camera.fieldOfView -= fovMin * Time.deltaTime / fovSpeed;
if(mainCamera.camera.fieldOfView < fovMin)
{
mainCamera.camera.fieldOfView = fovMin;
}
}
else
{
mainCamera.camera.fieldOfView += fovMax * Time.deltaTime / 0.5F;
if(mainCamera.camera.fieldOfView > fovMax)
{
mainCamera.camera.fieldOfView = fovMax;
}
}
//////////////////
// MOUSE1 SHOOT //
if(Input.GetMouseButtonDown(0))
{
ShootStuff();
}
}//UPDATE
IEnumerator ShootStuff()
{
animObject.animation["Shot"].speed = 3;
animObject.animation.Play("Shot");
audio.clip = soundFire;
audio.Play();
lastShot = Time.time;
yield return new WaitForSeconds(1);
animObject.animation["Shot"].speed = -3;
animObject.animation.Play("Shot");
}
void OnGUI()
{
GUI.color = colour;
GUI.skin.font = font;
GUI.Box(new Rect(Screen.width - 300, Screen.height-75,100,50), "CHARGES:" + "\n" + ammoCount);
}
}//END CLASS
Comment