Created
March 30, 2017 21:11
-
-
Save JoeValenti/0ee541ff1fa007b10888b494f3995439 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class MummyAI : MonoBehaviour { | |
| // Global variables | |
| int currentDir = 1; | |
| public float speed = 0.00f; | |
| public float chaseSpeed = 0.0f; | |
| Vector3 playerPos; | |
| Vector3 thisPos; | |
| public Animator mummyAnimator; | |
| public float chaseDist = 0.0f; | |
| bool chaseX = false; | |
| bool chaseY = false; | |
| // Use this for initialization | |
| void Start () { | |
| InvokeRepeating("Move", 1f, 1f); | |
| } | |
| // Update is called once per frame | |
| void Update () { | |
| //Find the player position | |
| playerPos = GameObject.FindGameObjectWithTag("Player").transform.position; | |
| thisPos = this.transform.position; | |
| //Determine if player is close enough to chase | |
| if (Mathf.Abs(thisPos.x - playerPos.x) < chaseDist) | |
| { | |
| chaseX = true; | |
| //change color to red | |
| GetComponent<SpriteRenderer>().color = new Color(255, 0, 0); | |
| //increase speed | |
| float step = chaseSpeed * Time.deltaTime; | |
| //chase player | |
| //transform.position = Vector3.MoveTowards(transform.position, playerPos, step); | |
| } | |
| else chaseX = false; | |
| if(Mathf.Abs(thisPos.y - playerPos.y) < chaseDist){ | |
| chaseY = true; | |
| GetComponent<SpriteRenderer>().color = new Color(255, 0, 0); | |
| float step = chaseSpeed * Time.deltaTime; | |
| //transform.position = Vector3.MoveTowards(transform.position, playerPos, step); | |
| } | |
| else { | |
| chaseY = false; | |
| } | |
| //chase the appropriate direction | |
| if(chaseX && !chaseY) | |
| { | |
| if(thisPos.x - playerPos.x > 0) | |
| { | |
| //chase left | |
| Vector3 position = this.transform.position; | |
| position.x -= chaseSpeed; | |
| this.transform.position = position; | |
| mummyAnimator.Play("mummyLeft"); | |
| }else | |
| { | |
| //chase right | |
| Vector3 position = this.transform.position; | |
| position.x += chaseSpeed; | |
| this.transform.position = position; | |
| mummyAnimator.Play("mummyRight"); | |
| } | |
| }else if(chaseY && !chaseX) | |
| { | |
| if(thisPos.y - playerPos.y > 0) | |
| { | |
| //chase up | |
| Vector3 position = this.transform.position; | |
| position.y -= chaseSpeed; | |
| this.transform.position = position; | |
| mummyAnimator.Play("mummyDown"); | |
| }else | |
| { | |
| //chase down | |
| Vector3 position = this.transform.position; | |
| position.y += chaseSpeed; | |
| this.transform.position = position; | |
| mummyAnimator.Play("mummyUp"); | |
| } | |
| }else if(chaseX && chaseY) | |
| { | |
| //Determine which direction is further | |
| float xDist, yDist; | |
| xDist = thisPos.x - playerPos.x; | |
| yDist = thisPos.y - playerPos.y; | |
| if(yDist < xDist) | |
| { | |
| //move horizontal | |
| if (thisPos.x - playerPos.x > 0) | |
| { | |
| //chase left | |
| Vector3 position = this.transform.position; | |
| position.x -= chaseSpeed; | |
| this.transform.position = position; | |
| mummyAnimator.Play("mummyLeft"); | |
| } | |
| else | |
| { | |
| //chase right | |
| Vector3 position = this.transform.position; | |
| position.x += chaseSpeed; | |
| this.transform.position = position; | |
| mummyAnimator.Play("mummyRight"); | |
| } | |
| }else if(xDist < yDist) | |
| { | |
| //Move vertical | |
| if (thisPos.y - playerPos.y > 0) | |
| { | |
| //chase up | |
| Vector3 position = this.transform.position; | |
| position.y -= chaseSpeed; | |
| this.transform.position = position; | |
| mummyAnimator.Play("mummyDown"); | |
| } | |
| else | |
| { | |
| //chase down | |
| Vector3 position = this.transform.position; | |
| position.y += chaseSpeed; | |
| this.transform.position = position; | |
| mummyAnimator.Play("mummyUp"); | |
| } | |
| } | |
| } | |
| //if player is too far away, then roam | |
| if (chaseX == false && chaseY == false) { | |
| //change to original color | |
| GetComponent<SpriteRenderer>().color = new Color(255, 255, 255); | |
| if (currentDir == 0) | |
| { | |
| Vector3 position = this.transform.position; | |
| position.y -= speed; | |
| this.transform.position = position; | |
| mummyAnimator.Play("mummyDown"); | |
| } | |
| else if (currentDir == 1) | |
| { | |
| Vector3 position = this.transform.position; | |
| position.x += speed; | |
| this.transform.position = position; | |
| mummyAnimator.Play("mummyRight"); | |
| } | |
| else if (currentDir == 2) | |
| { | |
| Vector3 position = this.transform.position; | |
| position.y += speed; | |
| this.transform.position = position; | |
| mummyAnimator.Play("mummyUp"); | |
| } | |
| else if (currentDir == 3) | |
| { | |
| Vector3 position = this.transform.position; | |
| position.x -= speed; | |
| this.transform.position = position; | |
| mummyAnimator.Play("mummyLeft"); | |
| } | |
| } | |
| } | |
| void Move(){ | |
| //pick a direction | |
| currentDir = Random.Range(0, 4); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment