So I’ve been trying to understand how yield works and whenever I believe I do my code just doesn’t work…
I have a GoTo action and if the character is sitting down I want him to stand up.
This is my code for the GoTo ()
function GoToAction () {
//Add StandUp (if player is sitting)
if (currentState == PlayerStates.SittingDown || previousState == PlayerStates.SittingDown){
yield StandUp();
}
var selectedPosition : Vector3;
if (currentAction.selectedDestination == null){
selectedPosition = currentAction.selectedPosition; //Used for going to a FLOOR position.
}
else {
selectedPosition = currentAction.selectedDestination.position;
}
GetComponent(NavMeshAgent).SetDestination(selectedPosition);
currentState = PlayerStates.GoingTo;
}
and here is the StandUp () that I would like to run if the player is sitting:
function StandUp(): IEnumerator {
if (playerProperties.isSitting){
playerProperties.isSitting = false; // trigger getUp anim
}
if (playerProperties.isSittingTable){
playerProperties.isSittingTable = false; // trigger getUp anim
currentAction.selectedObject.GetComponent(Obj_Properties_Class).sitProperties.animObj.GetComponent(Animator).SetBool("sitDown", false); //trigger standUp object anim
}
if (playerProperties.isLaying){
playerProperties.isLaying = false; // trigger getUp anim
}
//closestTrigger
currentSitTrigger.parent.GetComponent(Obj_Properties_Class).sitProperties.availableSitsArray.Add(currentSitTrigger);
currentSitTrigger = null;
currentState = PlayerStates.StandingUp;
if (animController.currentBaseState.nameHash != animController.idleState){
yield; //I would assume this holds this function until complete. Once terminated it goes back to the original GoTo()?
}
currentState = PlayerStates.Idle;
GetComponent(NavMeshAgent).enabled = true;
}
Right now nothing happens…whenever I have the yield on the GoToAction the function doesn’t do anything, even if I add a Print before it…
I just can’t figure out how to do this!
Any help very appreciated.