I'm using a state machine, and the following code is triggered when you enter the moving state. I'm using a couroutine because it has to keep running even if you exit the moving state.
My problem is that I have some spellbuttons, and my character move towards the buttons, when I click them, even though I check if the hotcontrol equals 0. I suspect it's because OnGUI(?) and FixedUpdate runs at different times, so hotcontrol doesn't equal 0 in the frame that I'm checking for it, but I really don't know what to do from here.
IEnumerator Moving_EnterState()
{
yield return new WaitForFixedUpdate();
if(!courotineInUse)
{
playerTargetPos = returnMousePosition();
transform.LookAt(playerTargetPos);
playerIsMoving = true;
sentFromMovingUpdate = false;
courotineInUse = true;
while(playerIsMoving == true)
{
Vector3 move = Vector3.MoveTowards(transform.position, playerTargetPos, movementSpeed*Time.deltaTime*100);
transform.position = move;
transform.position = new Vector3(transform.position.x, Terrain.activeTerrain.SampleHeight(transform.position)+0.7f, transform.position.z);
if(Mathf.Round(transform.position.x) == Mathf.Round(playerTargetPos.x) && Mathf.Round(transform.position.z) == Mathf.Round(playerTargetPos.z))
{
playerIsMoving = false;
currentState = States.Idle;
courotineInUse = false;
yield break;
}
yield return new WaitForFixedUpdate();
if(Input.GetMouseButtonDown(0) && GUIUtility.hotControl == 0)
{
if(casting == false)
{
yield return new WaitForFixedUpdate();
print("Pressed");
courotineInUse = false;
currentState = States.Moving;
yield break;
}
}
}
}
else
{
yield break;
}
}
↧