The following code [targetdown.cs]attached to an object ,name as target.
it have two animation, up and down , if some colliders touch this target,it will play "down" animation. If three seconds time up, it will play "up" animation.
the another code[win.cs] also attached the target. If consecutive hit two target, it will be generated a battery.
Now I want to write these functions in a class , not monobehavior. If when I need, I new a class to generated a target.
How to write a class to generate target all function?
public class targetdown : MonoBehaviour {
public GameObject targetRoot;
private bool beenHit = false;
private float timer = 0.0f;
public AudioClip hitsound;
public AudioClip resetSound;
void OnCollisionEnter(Collision theObject)
{
if(beenHit==false && theObject.gameObject.name=="Copycoconut")
{
audio.PlayOneShot(hitsound);
targetRoot.animation.Play("down");
beenHit=true;
CoCoWin.targets++;
}
}
void Update()
{
if(beenHit)
{
timer+=Time.deltaTime;
}
if(timer>3)
{
audio.PlayOneShot(resetSound);
targetRoot.animation.Play("up");
beenHit = false;
timer = 0.0f;
CoCoWin.targets=0;
}
}
[win.cs]
public class win : MonoBehaviour {
public static int targets = 0;
private bool haveWon = false;
public AudioClip winAudio ;
public GameObject battery ;
private GameObject launcher;
void Update ()
{
if (launcher == null)
{
launcher = GameObject.Find("Launcher");
}
if(targets == 2 && haveWon == false)
{
launcher.GetComponent<ObjectThrow>().CanThrow = false;
targets = 0;
audio.PlayOneShot(winAudio);
GameObject clone ;
clone = Instantiate(battery,new Vector3(transform.position.x,transform.position.y,transform.position.z),
transform.rotation) as GameObject;
clone.transform.localScale=new Vector3(100,100,100);
haveWon=true;
}
}
asked
Jul 19 '12 at 10:03 AM
NMNW
1
●
2
I guess I'm a bit lost as to why you want a class rather than a MonoBehaviour derivative. You are using Update etc... Can you explain why you need the class?
mmm, I just want to practice using the class, or can give some sample for class code?