// Start is called before the first frame update void Start() { description = "Smash " + numToDestroy + " items on floor " + level; destroyed = new List <Breakable>(); CHC = GameObject.Find("CentralHumanController").GetComponent <CentralHumanController>(); // Retrieve all breakable objects breakableObjects = CHC.allObjects[level - 1].Item1; }
void Start() { rb = GetComponent <Rigidbody>(); cl = GetComponent <Collider>(); // rb.centerOfMass = 0; collidePoint = transform.position; // + (rb.centerOfMass + Vector3.up * cl.bounds.size.y * 0.8f); toppled = false; CHC = GameObject.Find("CentralHumanController").GetComponent <CentralHumanController>(); CHC.registerObject(this.gameObject); KnockedSound = GetComponent <AudioSource>(); StartCoroutine(AudioLoadIn()); //Audio Engine if (_hasAudio) { KnockedSound.clip = objectKnock; } df = GetComponent <DestroyEffect>(); }
void Update() { if (pause) { return; } if (!hasTarget) { return; } // Register worker to CHC; code cannot be placed into Start because Gamemanager's CHC might not have been initialized yet // there are better ways to do this if (CHC == null) { // Register itself at GameManager CHC = GameManager.instance.CHC; CHC.humans.Add(this); breakableObjects = CHC?.allObjects[level - 1].Item1; knockableObjects = CHC?.allObjects[level - 1].Item2; return; } // I don't know who put this here but get rid of it!!! It messes up the workers when the raccoon leaves the floor!!! //if (!onSameFloor(target)) // return; _timer += Time.deltaTime; if (alreadyPlayed && _timer > replayInterval) { alreadyPlayed = false; } seesRaccoon = inFOV(agent, transform, target, maxAngle, maxRadius); // Animation anim.SetBool("scared", seesRaccoon); // 'lastKnownLocation' is assigned by the Central Human Controller. If a new 'lastKnownLocation' is assigned, then the raccoon has been spotted // somewhere and the worker will head to that location if he can reach it // Debug Animation //if (GameObject.Find("C_worker_Rigged").transform == transform) //{ //Debug.Log(agent.CalculatePath(lastKnownLocation, p)); //Debug.Log(p.status); //} // When the worker is at the same floor and not reach lastKnownLocation yet if (CHC.spotted && agent.CalculatePath(lastKnownLocation, p) && System.Math.Abs(transform.position.x - lastKnownLocation.x) > 2 && System.Math.Abs(transform.position.z - lastKnownLocation.z) > 2 && onSameFloor(target)) { //Debug.Log("Now chasing Raccoon"); chasing = true; //searching = false; investigating = false; idle = false; anim.SetBool("idle", false); //agent.ResetPath(); agent.SetDestination(lastKnownLocation); //Audio trigger for sighting Raccoon if (!seesRaccoon && !alreadyPlayed) { chaseVO(); } // Animation anim.SetBool("chasing", true); anim.SetBool("scared", false); } //Debug.Log("(Worker " + GetInstanceID().ToString() + ") Distance(x): " + System.Math.Abs(transform.position.x - lastKnownLocation.x).ToString() // + ", Distance(z): " + System.Math.Abs(transform.position.z - lastKnownLocation.z).ToString()); //Debug.Log("(Worker " + GetInstanceID().ToString() + ") seesRaccoon: " + seesRaccoon); /* * // If the raccoon leaves the nav mesh, the worker will return to the 'idle' state * if (!agent.pathPending) * { * Debug.Log("Raccoon not in nav mesh"); * chasing = false; * searching = false; * idle = true; * anim.SetBool("idle", true); * } */ // The worker will stop if he is at 'lastKnownLocation' and can't see the raccoon if (!seesRaccoon && System.Math.Abs(transform.position.x - lastKnownLocation.x) <= 4 && System.Math.Abs(transform.position.z - lastKnownLocation.z) <= 4) { //Debug.Log("Lost Raccoon"); // If the raccoon is nearby, the worker will still chase her if (System.Math.Abs(transform.position.x - target.position.x) <= 4 && System.Math.Abs(transform.position.z - target.position.z) <= 4) { transform.LookAt(target); } // Otherwise, the worker will go idle else { chasing = false; investigating = false; idle = true; agent.ResetPath(); // Animation anim.SetBool("chasing", false); } } if (seesRaccoon && System.Math.Abs(transform.position.x - lastKnownLocation.x) <= 2 && System.Math.Abs(transform.position.z - lastKnownLocation.z) <= 2 && canAttack) { // Stun attack here //Debug.Log("stun attack used"); target.GetComponent <RaccoonController>().StunRaccoon(lastKnownLocation - transform.position); anim.SetBool("attack", true); // Start cooldown after attack canAttack = false; attackCooldown = 10.0f; //Audio if (!canAttack) { stunVO(); } // Animation //anim.Play("kicking"); } else if (!canAttack) { if (attackCooldown > 0) { attackCooldown -= Time.deltaTime; } else { canAttack = true; //Debug.Log("stun attack ready"); } anim.SetBool("attack", false); } /* * // The worker will turn to his left and right in case the raccoon is beside him * if (searching && !chasing && !idle) * { * * Debug.Log("Searching for Raccoon"); * agent.isStopped = true; * transform.Rotate(new Vector3(0.0f, 90.0f, 0.0f), Time.deltaTime * rotationSpeed); * transform.Rotate(new Vector3(0.0f, 270.0f, 0.0f), Time.deltaTime * rotationSpeed); * transform.Rotate(new Vector3(0.0f, 270.0f, 0.0f), Time.deltaTime * rotationSpeed); * agent.isStopped = false; * * searching = false; * idle = true; * anim.SetBool("idle", true); * } */ // For each breakable object on this worker's floor for (int i = 0; i < breakableObjects.Count; i++) { // If the object was destroyed if (breakableObjects[i].destroyed) { destroyedObjects.Add(breakableObjects[i]); //Debug.Log(breakableObjects[i].ToString() + " was destroyed. Distance from worker " + id + ": " + Vector3.Distance(breakableObjects[i].transform.position, transform.position).ToString()); // If this worker heard the object being destroyed and is not chasing the raccoon if (!chasing && Vector3.Distance(breakableObjects[i].transform.position, transform.position) < hearingRadius) { investigating = true; //searching = false; idle = false; anim.SetBool("idle", false); lastKnownLocation = breakableObjects[i].transform.position; agent.SetDestination(lastKnownLocation); //Debug.Log("Worker " + id + " heard object " + breakableObjects[i].ToString() + " being destroyed. Now heading to " + breakableObjects[i].transform.position.ToString() + " to investigate"); } } } // Remove all objects that have been destroyed from 'breakableObjects' for (int i = 0; i < destroyedObjects.Count; i++) { breakableObjects.Remove(destroyedObjects[i]); Debug.Log(destroyedObjects[i].ToString() + " has been removed"); } destroyedObjects.Clear(); // For each knockable object on this worker's floor for (int i = 0; i < knockableObjects.Count; i++) { // If the object was knocked over if (knockableObjects[i] && knockableObjects[i].toppled) { toppledObjects.Add(knockableObjects[i]); // Debug.Log(knockableObjects[i].ToString() + " was knocked over. Distance from worker " + id + ": " + Vector3.Distance(knockableObjects[i].transform.position, transform.position).ToString()); // If this worker heard the object being knocked over and is not chasing the raccoon if (!chasing && Vector3.Distance(knockableObjects[i].transform.position, transform.position) < hearingRadius) { investigating = true; //searching = false; idle = false; anim.SetBool("idle", false); lastKnownLocation = knockableObjects[i].transform.position; agent.SetDestination(lastKnownLocation); // Debug.Log("Worker " + id + " heard object " + knockableObjects[i].ToString() + " being knocked over. Now heading to " + knockableObjects[i].transform.position.ToString() + " to investigate"); } } } for (int i = 0; i < toppledObjects.Count; i++) { knockableObjects.Remove(toppledObjects[i]); // Debug.Log(toppledObjects[i].ToString() + " has been removed"); } toppledObjects.Clear(); // If the worker does not see the raccoon and is not near his starting position if (!seesRaccoon && (System.Math.Abs(transform.position.x - initialPosition.x) > 2 || System.Math.Abs(transform.position.z - initialPosition.z) > 2) && agent.velocity == Vector3.zero) { // Start coroutine to return the worker to the idle state //Debug.Log("(Worker " + GetInstanceID().ToString() + ") may be stuck. Now running the 'isStuck' Coroutine"); StartCoroutine(isStuck()); } // The worker will return to his original position if he can't find the raccoon if (idle) { if (System.Math.Abs(transform.position.x - initialPosition.x) <= 2 && System.Math.Abs(transform.position.z - initialPosition.z) <= 2) { //Debug.Log("In starting position. current position: " + transform.position.ToString() + " initial position: " + initialPosition.ToString()); transform.rotation = Quaternion.RotateTowards(transform.rotation, initialDirection, rotationSpeed); anim.SetBool("idle", true); } else if (transform.position != initialPosition) { //Debug.Log("Can't find Raccoon. Returning to initial position"); if (agent.SetDestination(initialPosition)) { anim.SetBool("idle", false); //Debug.Log("Now heading to " + agent.destination.ToString() + ". Initial position is " + initialPosition.ToString()); } } } if (anim.GetBool("scared")) { agent.isStopped = true; } else { agent.isStopped = false; } if (anim.GetBool("attack")) { agent.isStopped = true; } else { agent.isStopped = false; } }