void OnTriggerEnter(Collider col) { //this need to happen after the other object registers what it touched //so maybe a coroutine would help //as it is it will always report the last thing it triggered not the current //rather than a coroutine the object colliding could be the one making the decisions //it could have the trigger and it could tell what it collides with what to do //e.g. if col.tag is enemy, then getComponent<enemyScript>() //then tell the script to run its behaviour method which could be // myBehaviour(Collider){} it receives a collider type and behaves differently //depending which collider, it is given, or a string with the colliders name could be //given myColLastHit = col.gameObject.GetComponent <ILastTouched>().iLastEntered; if (myColLastHit is SphereCollider) { currentBehaviour = enemyBehaviour.attack; } else if (myColLastHit is BoxCollider) { currentBehaviour = enemyBehaviour.retreat; } else if (myColLastHit is CapsuleCollider) { currentBehaviour = enemyBehaviour.agro; } Debug.Log(currentBehaviour); }
void DecideBehaviour() { if (Random.Range(1, 30) < 5) { if (thisBehaviour == enemyBehaviour.DirectEngagement) { thisBehaviour = enemyBehaviour.Flank; } else { thisBehaviour = enemyBehaviour.DirectEngagement; } } }
void helpAlly() { // find all nearby allies that are in combat enemyBehaviour[] allies = FindObjectsOfType <enemyBehaviour>(); List <enemyBehaviour> alliesInCombat = new List <enemyBehaviour>(); foreach (enemyBehaviour ally in allies) { if (checkLOS(ally.transform.position) && ally.isInCombat()) { alliesInCombat.Add(ally); } } if (alliesInCombat.Count == 0) { // Shouldn't happen, but just in case return; } // find nearest ally enemyBehaviour nearestAlly = null; float dist = Mathf.Infinity; foreach (enemyBehaviour ally in alliesInCombat) { // compare the distance of this ally to the current closest found float new_dist = (ally.transform.position - transform.position).magnitude; if (new_dist < dist) { nearestAlly = ally; dist = new_dist; } } // Head towards the closest point we can to make LOS with player for (int i = 0; i < ASSIST_SEARCH_RESOLUTION; ++i) { // Scan up the friend's player-LOS to find the nearest point without an obstruction Vector3 check_pos = Vector3.Lerp(nearestAlly.getLastSeenPos(), nearestAlly.transform.position, (float)i / ASSIST_SEARCH_RESOLUTION); if (checkLOS(check_pos, true)) { // Found an unobstructed route to our ally's player-LOS last_player_pos = check_pos; had_LOS = true; break; } } }
// Update is called once per frame void Update() { slidey.value = health; if (health <= 0 && brokenDown == false) { brokenDown = true; gameController.score += gameController.generatorBonus; health = 0; slidey.gameObject.SetActive(false); nearestHuman = FindHuman(); print(nearestHuman); eb = nearestHuman.GetComponent <enemyBehaviour>(); eb.behaviourState = 2; eb.generator = this.gameObject; } }
void Update() { //Pathfinding demo if (Input.GetMouseButtonDown(1)) { //Convert mouse click point to grid coordinates Vector2 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); Point gridPos = WorldToGrid(worldPos); if (gridPos != null) { if (gridPos.X > 0 && gridPos.Y > 0 && gridPos.X < Width && gridPos.Y < Height) { //Convert player point to grid coordinates Point playerPos = WorldToGrid(Player.transform.position); Nodes[playerPos.X, playerPos.Y].SetColor(Color.blue); //Find path from player to clicked position BreadCrumb pathToFollow = PathFinder.FindPath(this, playerPos, gridPos); enemyBehaviour mov = Player.GetComponent <enemyBehaviour> (); mov.positions.Clear(); int count = 0; LineRenderer lr = Player.GetComponent <LineRenderer> (); lr.SetVertexCount(100); //Need a higher number than 2, or crashes out lr.SetWidth(0.1f, 0.1f); lr.SetColors(Color.yellow, Color.yellow); //Draw out our path while (pathToFollow != null) { lr.SetPosition(count, Grid.GridToWorld(pathToFollow.position)); mov.positions.Add(Grid.GridToWorld(pathToFollow.position)); pathToFollow = pathToFollow.next; count += 1; } lr.SetVertexCount(count); } } } }
public void updateBehaviour(Collider myOwnCollider, bool myOwnColliderWasJustExited) { if (myOwnCollider is SphereCollider) { currentBehaviour = enemyBehaviour.attack; } else if (myOwnCollider is BoxCollider) { currentBehaviour = enemyBehaviour.retreat; } else if (myOwnCollider is CapsuleCollider) { currentBehaviour = enemyBehaviour.agro; } if (myOwnColliderWasJustExited) { currentBehaviour--; } Debug.Log(currentBehaviour); }
void OnTriggerExit(Collider col) { myColLastExited = col.gameObject.GetComponent <ILastTouched>().iLastExited; if (myColLastExited is CapsuleCollider) { currentBehaviour = enemyBehaviour.none; } if (myColLastExited is SphereCollider) { currentBehaviour = enemyBehaviour.agro; } else if (myColLastExited is BoxCollider) { currentBehaviour = enemyBehaviour.attack; } Debug.Log(currentBehaviour); }
// Use this for initialization void Start() { enemyShips = GameObject.FindObjectOfType <enemyBehaviour>(); }