- Home /
Function running even though bool is false?
Hello! I am trying to create an enemy that can only swing every five or so seconds, so the player has a moment to attack. I thought that if every time the enemy swung, the script would run a coroutine and set a bool to false, while one of the requirements of the attack function working is that the bool be true. After five seconds, the bool would be reneabled, and the enemy would be able to attack again. For some reason, even if the bool is not true, the enemy still attacks, without slowing down a bit. Any help would be appreciated!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
public class MovingScript : MonoBehaviour {
private Animator anim;
public GameObject player;
public float dist;
//active runs the corrutine
public bool active;
//deactive keeps the corrutine from rerunning
public bool deactive;
public float movespeed = 10f;
public Transform player2;
public int health;
public bool blocked;
public bool swung;
// Use this for initialization
void Start() {
active = true;
anim = GetComponent<Animator>();
deactive = false;
player = GameObject.FindGameObjectWithTag("Player");
health = 100;
if (active == true)
{
StartCoroutine(Run());
}
}
// Update is called once per frame
void Update() {
float dist = Vector3.Distance(player.transform.position, transform.position);
if (deactive == false)
{
anim.Play("CombatIdle");
}
if (deactive == true && dist >= 3)
{
transform.LookAt(player.transform);
transform.GetComponent<CharacterController>().SimpleMove(transform.forward * movespeed * Time.deltaTime);
anim.Play("Walk");
}
else if (deactive == true && dist <= 3 && blocked == false)
{
anim.Play("Attack");
StartCoroutine(blocking());
}
}
IEnumerator Run()
{
//five seconds of dialogue
yield return new WaitForSeconds(5);
deactive = true;
active = false;
}
IEnumerator blocking()
{
deactive = false;
yield return new WaitForSeconds(5);
deactive = true;
}
}
Yikes! Not sure what happened there. I was confused at what you meant at first, i’m not sure what went wrong with the formatting. I’ll fix that ASAP.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Value not changing back to where its supposed to... 0 Answers
Particleellipsoidemitter is missing from Unity 5 library 0 Answers