// Use this for initialization protected override void Start() { rb = GetComponent <Rigidbody2D>(); Behaviours = new States(); Behaviours = States.patrolling; //areaOfMovement = Random.insideUnitCircle * 2 + Enemy; }
// Use this for initialization protected override void Start() { ai1 = gameObject.AddComponent <AI>(); behaviours = new States(); behaviours = States.patrolling; constantYPos = transform.position.y; ai1.t = transform; }
// Update is called once per frame protected override void Update() { /*switch (state) * { * case AI_STATE.STATE_IDLE: * * /// do idle state stuff * /// // IdleState(); * break; * * case AI_STATE.STATE_ATTACK: * * //AttackState(); * break; * * // etc * } */ //Setting raycast1 so the enemy can detect the player /*ai1.DetectingPlayer(pointEnd.position,transform.right, player, sightRange, AttackRange, behaviours); * Debug.DrawRay(transform.position, transform.right, Color.red); */ //To detect player RaycastHit2D hit = Physics2D.Raycast(RaycastPoint.position, transform.right, sightRange); Debug.DrawLine(RaycastPoint.position, hit.point, Color.red); //Edge detection raycast RaycastHit2D groundDetect = Physics2D.Raycast(pointEnd.position, Vector2.down); Debug.DrawLine(pointEnd.position, groundDetect.point, Color.blue); // hit object for edge detection stored in transform variable Transform Object = groundDetect.transform; // hit object for player detection stored in transform variable Transform DetectedObject = hit.transform; transform.position = new Vector3(transform.position.x, constantYPos, 0); //behaviours = ai1.DetectingPlayer(RaycastPoint.position, transform.right, sightRange, AttackRange, foundPlayer); //Note: Maximum sight range may be at 5 since 10 may have case problems switch (behaviours) { //The AI will move to the right and have it material colour set to green case States.patrolling: transform.Translate(Vector2.right * speed * Time.deltaTime); materal.GetComponent <Renderer>().material.color = Color.green; break; //When the enemy spots the player it will follow the player and change it's material colour to yellow //Also the enemy will rotate 180s depending on its position case States.moving: materal.GetComponent <Renderer>().material.color = Color.yellow; transform.position = Vector2.Lerp(transform.position, player.position, Time.deltaTime); //Use rotate method in AI class if (transform.position.x < lastPos.x) { lastPos.x = transform.position.x + 0.01f; transform.eulerAngles = new Vector3(0, 180, 0); } else if (transform.position.x > lastPos.x) { lastPos.x = transform.position.x - 0.01f; transform.eulerAngles = new Vector3(0, 0, 0); } break; //when the enemy is attacking its material colour will change to red //and it will access the health variable in the playermovement script case States.attacking: materal.GetComponent <Renderer>().material.color = Color.red; healthbar = hit.transform.GetComponent <PlayerMoverment1>(); //The player's health will reduced every three seconds if (Time.time > attackDelay) { attackDelay = Time.time + 3; healthbar.health -= 100; } break; } //Turning left if the enemy is moving right if (transform.eulerAngles.y == 0 && canTurn == true) { transform.eulerAngles = new Vector3(0, 180, 0); canTurn = false; } //Turning right if the enemy is moving left if (transform.eulerAngles.y == 180 && canTurn == true) { transform.eulerAngles = new Vector3(0, 0, 0); canTurn = false; } //If the enemy loses sight of the player then it will go back to patrolling if (foundPlayer == false) { behaviours = States.patrolling; } //The if statement will see if the detected Object exists if (DetectedObject != null) { //Debug.Log(DetectedObject.name); //The if statement below will see if the detected object has the Player tag if (DetectedObject.CompareTag("Player")) { //Debug.Log("Found Player"); behaviours = States.moving; foundPlayer = true; if (Vector2.Distance(DetectedObject.position, transform.position) < AttackRange) { behaviours = States.attacking; } //If the player is invisible then the enem's state will be patrolling if (DetectedObject.GetComponent <Stealth>().isInvisible == true) { behaviours = States.patrolling; } } else { foundPlayer = false; } /*if (DetectedObject.name == "Tilemap") * { * canTurn = true; * } */ } if (groundDetect.collider != null) { if (groundDetect.distance > 2) { //The enemy will scan for the edge of the platform once every second if (Time.time > nextScan) { nextScan = Time.time + 0.2f; canTurn = true; } //If the enemy has found the player but is on or moving to another platofrm //then the enemy will go back to patrolling if (foundPlayer == true) { behaviours = States.patrolling; } } } }
// Update is called once per frame protected override void Update() { Player = player.position; Enemy = transform.position; direction = Player - Enemy; hit = Physics2D.Raycast(Enemy, direction); Debug.DrawLine(Enemy, Player, Color.red); //Ain bow at player Bow.rotation = Quaternion.Slerp(Bow.rotation, angle, Time.deltaTime * rotateSpeed); switch (Behaviours) { case States.patrolling: //Telling the enemy where to move to at a speed of 4 transform.position = Vector3.MoveTowards(Enemy, newPos, Time.deltaTime); DestLength = Vector2.Distance(Enemy, newPos); //In the if statement the enemy will find one position to move to if it hasn't alread found one if (noDestination == true) { //Setting boundaries for where the enemy can go within a sphere with a radius of 2 newPos = Random.insideUnitCircle * 5 + Enemy; noDestination = false; } //find another position to move to once it's at the previous selected position if (Vector3.Distance(newPos, Enemy) < 0.1f) { noDestination = true; } /*if (transform.position.y < player.position.y) * { * noDestination = true; * } */ //Resets the bow to original rotation angle = Quaternion.Euler(0, 0, 0); //switch between left and right sprite when play moves left or right if (transform.position.x > lastPos.x) { lastPos.x = transform.position.x + 0.01f; //transform.eulerAngles = new Vector3(0, 180, 0); } else if (transform.position.x < lastPos.x) { lastPos.x = transform.position.x - 0.1f; //transform.eulerAngles = new Vector3(0, 0, 0); } break; case States.attacking: //Calculates an angle for the enemy's bow to rotate to angle = Quaternion.Euler(0, 0, Mathf.Atan2(direction.normalized.y, direction.normalized.x) * Mathf.Rad2Deg); //Arrows will be fired depending on the fire rate if (Time.time > FireNext) { FireNext = Time.time + fireArrowsDelay; //The created object is stored into a object variable called arrow GameObject arrow = Instantiate(arrows, FirePoint.position, Quaternion.identity); //If the arrow gameobject exists it will apply force in the direction of the firepoint object if (arrow != null) { arrow.GetComponent <Rigidbody2D>().AddForce(FirePoint.right * 5, ForceMode2D.Impulse); } } //Switch between left and right sprite depending on the angle of the bow if (Bow.eulerAngles.z > 270) { transform.eulerAngles = new Vector3(0, 180, 0); } else if (Bow.eulerAngles.z < 270) { transform.eulerAngles = new Vector3(0, 0, 0); } break; } //The transform of the object that was hit is stored in the transform variable object Transform Object = hit.transform; if (Object != null) { //When finding the player the enemy will attack else the enemy will continue to patrol if (Object.CompareTag("Player")) { Behaviours = States.attacking; //If the enemy is invisiable the enemy will patrol if (Object.GetComponent <Stealth>().isInvisible == true) { Behaviours = States.patrolling; } } else { Behaviours = States.patrolling; } } }