using System.Collections; using System.Collections.Generic; using UnityEngine; public class Gamification_PlayerMove : MonoBehaviour { Vector2 targetPosition = new Vector2(0, 0); Animator animethis; public GameObject objModel, objModelCant; Transform objEyes; bool front = true; public static Gamification_PlayerMove instance; SpriteRenderer zoeHead, zoeBody; List zoeEyes = new List(); public Sprite headFront, bodyFront, headBack, bodyBack, eyesNormal, eyesHappy; private void Awake() { instance = this; targetPosition = transform.position; } // Start is called before the first frame update void Start() { animethis = GetComponent(); zoeHead = transform.Find("ZoeHead").GetComponent(); zoeBody = GetComponent(); objEyes = transform.Find("ZoeHead").Find("Eyes"); zoeEyes.AddRange(objEyes.GetComponentsInChildren()); StartCoroutine(Blink()); } IEnumerator Blink() { while (true) { yield return new WaitForSecondsRealtime(2 + (Random.Range(0, 20) / 10)); animethis.SetTrigger("blink"); } } // Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { if (CheckClick()) { targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); GameObject objcreated = Instantiate(objModel, null); objcreated.transform.position = targetPosition; Destroy(objcreated, 1); if (targetPosition.y > transform.position.y) { ChangeSprites(1); } else { ChangeSprites(0); } animethis.SetBool("walking", true); } else { var faketargetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); GameObject objcreated = Instantiate(objModelCant, null); objcreated.transform.position = new Vector3(faketargetPosition.x, faketargetPosition.y, 0); Destroy(objcreated, 1); } } if (targetPosition == (Vector2)transform.position) { animethis.SetBool("walking", false); if (animethis.GetBool("startgame")) { ChangeSprites(0); } } } bool CheckClick() { RaycastHit2D thisOut = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); if (thisOut) { if (thisOut.collider.name == "PlayableArea" || thisOut.collider.GetComponent()) { return true; } else { return false; } } else { return false; } } void ChangeSprites(int direction) { switch (direction) { case 0: zoeHead.sprite = headFront; zoeBody.sprite = bodyFront; if (!front) { animethis.SetTrigger("front"); front = true; objEyes.rotation = Quaternion.Euler(new Vector3(0, 0, 0)); } break; case 1: zoeHead.sprite = headBack; zoeBody.sprite = bodyBack; if (front) { animethis.SetTrigger("back"); front = false; objEyes.rotation = Quaternion.Euler(new Vector3(0, 90, 0)); } break; default: break; } } private void FixedUpdate() { transform.position = Vector2.MoveTowards(transform.position, targetPosition, .1f); } public void SetAnime(bool ready) { animethis.SetBool("startgame", ready); if (ready) { //animethis.SetTrigger("front"); ChangeSprite(eyesHappy); } else { ChangeSprite(eyesNormal); } } public void ChangeSprite(Sprite spr) { foreach (var item in zoeEyes) { item.sprite = spr; } } }