The mentioned code for everyone's perusal, silly comments, naming conventions, debug logs and all.
using UnityEngine;
using System.Collections;
public class RideTheDerp : MonoBehaviour {
public Behaviour[] RidingBehaviours;
public Behaviour[] IdleBehaviours;
private bool activenow = false;
public Camera DerpCam;
public Transform dismountLocation;
public Transform riderLocation;
public GameObject riderPrefab;
private GameObject player;
// Use this for initialization
void Start () {
//CHANGE THIS LATER!!!...this may not work for instantiated Derplidocus's...if they are
//instatiated when there is no "Player" tag object will lead to BAD THINGS!
//player = GameObject.FindGameObjectWithTag ("Player");
foreach(Behaviour vb in RidingBehaviours){
vb.enabled = false;
}
DerpCam.enabled = false;
activenow = false;
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("E") && activenow) {
//if (activenow){
//activenow = false;
foreach(Behaviour vb in RidingBehaviours){
vb.enabled = false;
}
foreach(Behaviour vb in IdleBehaviours){
vb.enabled = true;
}
//GameObject Rider = riderLocation.transform.GetChild(0);
//Destroy(Rider);
foreach (Transform child in riderLocation) {
GameObject.Destroy(child.gameObject);
}
DerpCam.tag = "Untagged";
DerpCam.enabled = false;
player.transform.position = dismountLocation.transform.position;
player.transform.rotation = dismountLocation.transform.rotation;
player.SetActive(true);
activenow = false;
}
}
void Interact(){
Debug.Log ("Honk");
//player = GameObject.FindGameObjectWithTag ("Player");
player = GameObject.Find("Player");
if (activenow == false) {
//activenow = true;
foreach(Behaviour vb in RidingBehaviours){
vb.enabled = true;
}
foreach(Behaviour vb in IdleBehaviours){
vb.enabled = false;
}
GameObject Rider = (GameObject)Instantiate(riderPrefab, riderLocation.transform.position, riderLocation.transform.rotation);
Rider.transform.parent = riderLocation;
DerpCam.tag = "MainCamera";
DerpCam.enabled = true;
player.SetActive(false);
//activenow = true;
StartCoroutine(delayedPositive());
}
}
IEnumerator delayedPositive(){
yield return new WaitForSeconds (.1f);
activenow = true;
}
}
Now that I'm looking at it after a few months I'm thinking that the player was hopping off because I put the dismount code in the update and not in it's own method tbh.
But yeah that's how I last used coroutines...the coroutine method is at the bottom of the class and it's implemented at the tail-end of the Interact method. If you don't call upon the 'delayedPositive' and uncomment out the 'activenow = True;' in the interact method you can experience the quirk first-hand.
Granted it looks like a band-aid fix to me now that I can think of a better way to do this (without using coroutines at all), but laughing at old code's inefficiencies/silliness is a good sign I hear haha.
Hmmm...now I'm kinda at a loss for good places to use coroutines aside from...
The real beauty of coroutines is that they run "outside" the updates, and therefore they don't impact performances. Consider some risks they have, i.e. you will have to handle the sync with the update manually if that is needed, and also as already mentioned, consider the risk of having them hanging across time for too long.
That makes a lot of sense and is probably gonna come in handy to know.
