void Wander() { //put navmesh to go from left to right //find distance between Player and enemy GameObject[] particleshots = GameObject.FindGameObjectsWithTag("ParticleShot"); if (Input.GetButtonDown("Fire2") && playerDist <= proximityRadius) { curState = SeekerState.ATTACK; agent.destination = playerPosition; return; } else if (particleshots.Length > 0) { curState = SeekerState.IDLE; agent.destination = particleshots[particleshots.Length - 1].transform.position; return; } else { Vector3 randomDirection = UnityEngine.Random.insideUnitCircle * defaultRadius; randomDirection += transform.position; NavMeshHit navHit; NavMesh.SamplePosition(randomDirection, out navHit, defaultRadius, NavMesh.AllAreas); agent.destination = navHit.position; curState = SeekerState.IDLE; } }
void OnTriggerExit(Collider other) { if (other.gameObject.tag == "Player") { curState = SeekerState.IDLE; //then play the attack animation } }
// Update is called once per frame void Update() { GameObject[] particleshots = GameObject.FindGameObjectsWithTag("ParticleShot"); playerPosition = player.transform.position; playerDist = (playerPosition - transform.position).magnitude; if (curState == SeekerState.IDLE) { Wander(); } if (curState == SeekerState.ATTACK) { //Wander(); if (playerDist < attackdist) { Attack(); } else { //agent.destination = playerPosition; if (particleshots.Length > 0) { curState = SeekerState.IDLE; agent.destination = particleshots[particleshots.Length - 1].transform.position; } } } else { curState = SeekerState.IDLE; } }
void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "Player") { curState = SeekerState.ATTACK;//then play the attack animation // StartCoroutine(Damage()); } }
IEnumerator MoveToDestination(List <Node> _path, SeekerState state) { // Face and move towards point float maxTimer = 10.0f; float twoSeconds = 0.0f; Node lastNode = _path[_path.Count - 1]; bool init = true; foreach (Node n in _path) { Vector3 truePoint = new Vector3(n.worldPosition.x, transform.position.y, n.worldPosition.z); while (Vector3.Distance(transform.position, truePoint) > 0f) { Quaternion rotation = Quaternion.LookRotation(truePoint - transform.position); transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(truePoint - transform.position), rotSpeed * Time.deltaTime); transform.position = Vector3.MoveTowards(transform.position, truePoint, moveSpeed * Time.deltaTime); yield return(null); } if (state == SeekerState.Wander) { // look around when we reach the last node while (n == lastNode) { // Initalize finding a random angle to check if (init) { init = CheckAround(init); } else { CheckAround(init); } timer += Time.deltaTime; // After 10 seconds, go to a new destination if (timer > maxTimer) { checkingDone = true; // Look around every 2 seconds } else if (Mathf.Floor(timer) % 2.0f != twoSeconds) { init = true; } yield return(null); } } else if (state == SeekerState.Hide && n == lastNode) { atSpot = true; } } }
// Update is called once per frame void Update() { playerPosition = player.transform.position; playerDist = (playerPosition - transform.position).magnitude; if (playerDist <= AttackRange) { curState = SeekerState.ATTACK; } else { curState = SeekerState.IDLE; } if (curHealth <= 0) { curState = SeekerState.DEATH;//then play the death animation GameObject.Instantiate(PowerUp, transform.position, transform.rotation); Destroy(gameObject); } if (curState == SeekerState.IDLE) { Wander(); } else if (curState == SeekerState.ATTACK) { if (PlayerCharacter.pc.healthPoint > 0) { Attack(); } } }
void Update() { // Basic State Machine switch (_currentState) { // In this state, seeker will wait 10 seconds before looking for hidden players, this state only happens once in the life of a seeker case SeekerState.Wait: { if (wait) { float maxWait = 5f; waitTimer += Time.deltaTime; Debug.Log("Time to start: " + Mathf.Floor(waitTimer)); if (waitTimer >= maxWait) { wait = false; gameObject.AddComponent <SeekerCollision>(); _currentState = SeekerState.Wander; } } break; } // In this state, seeker will move to random spots case SeekerState.Wander: { Debug.Log("CURRENT STATE : WANDER."); // Check for hidden player if (CheckForHidden()) { if (moving) { StopCoroutine(movement); } pathfinding = true; destinationSet = false; moving = false; checkingDone = false; _currentState = SeekerState.Chase; } else { // set destination if (!destinationSet) { timer = 0.0f; destination = SetDestination(floor); if (Pathfind(transform.position, destination)) { destinationSet = true; movement = StartCoroutine(MoveToDestination(grid.path, _currentState)); } } // At last node of path and finished checking around, reset all bools and start new destination if (checkingDone) { StopCoroutine(movement); destinationSet = false; checkingDone = false; } } break; } // In this state, go to target until collision, once colliding this seeker will case SeekerState.Chase: { moveSpeed = 3f; Debug.Log("CURRENT STATE : CHASE."); if (hide) { _currentState = SeekerState.Hide; } else { //pathfind to hidden player if (pathfinding) { pathfinding = Pathfind(transform.position, ChaseDestination(targetPosition)); if (!pathfinding) { moving = false; } } //trace path made if (!moving) { movement = StartCoroutine(MoveToDestination(grid.path, _currentState)); moving = true; } if (moving) { Debug.DrawRay(transform.position, (targetPosition - transform.position).normalized * Vector3.Distance(transform.position, targetPosition), Color.green); } } break; } // In this state, go to a random location, become hidden and remove the properties of a seeker case SeekerState.Hide: { moveSpeed = 3.5f; Debug.Log("CURRENT STATE : HIDE"); if (!destinationSet) { destination = SetDestination(floor); Node tempDestination = grid.PointOnGrid(destination); if (tempDestination.isObstacle) { destination = SetDestination(floor); } else { destinationSet = true; pathfinding = true; } } // Pathfind if (pathfinding) { pathfinding = Pathfind(transform.position, destination); if (!pathfinding) { moving = false; } } // Trace path made if (!moving) { movement = StartCoroutine(MoveToDestination(grid.path, _currentState)); moving = true; } if (atSpot) { gameObject.GetComponent <Renderer>().material.color = mHide.color; gameObject.tag = "Hidden"; gameObject.name = "Hider"; Destroy(GetComponent <Seek>()); } break; } } }
// Use this for initialization void Start() { player = GameObject.FindWithTag("Player"); curState = SeekerState.IDLE; agent = GetComponent <NavMeshAgent>(); }
public void UpdateState(float currentTime) { // if we reach the goal before being tagged, switch to atGoal state if (State == SeekerState.Running) { float baseDistance = Vector3.Distance(Position, Globals.HomeBaseCenter); if (baseDistance < (Radius + Globals.HomeBaseRadius)) State = SeekerState.AtGoal; } // update lastRunningTime (holds off reset time) if (State == SeekerState.Running) { lastRunningTime = currentTime; } else { float resetDelay = 4; float resetTime = lastRunningTime + resetDelay; if (currentTime > resetTime) { // xxx a royal hack (should do this internal to CTF): Demo.QueueDelayedResetPlugInXXX(); } } }
// reset state public override void Reset() { base.Reset(); BodyColor = new Color((byte)(255.0f * 0.4f), (byte)(255.0f * 0.4f), (byte)(255.0f * 0.6f)); // blueish Globals.Seeker = this; State = SeekerState.Running; evading = false; }