// Update is called once per frame void Update() { if (!GetComponentInChildren <Weapon>().Attacking) { Vector3 vpos3 = Camera.main.WorldToScreenPoint(transform.position); // Get screen position of avatar float angle = Vector2.SignedAngle(new Vector2(0, 1), Input.mousePosition - vpos3); // Calculate angle of mouse and front transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle)); } if (Input.GetKeyDown(KeyCode.LeftShift))// Press shift to crush { StartCoroutine(PresetScript.Crush()); } // Movement if (Input.GetKey(KeyCode.W)) { transform.Translate(Vector2.up * PresetScript.moveSpeed * Time.deltaTime, Space.World); } if (Input.GetKey(KeyCode.S)) { transform.Translate(Vector2.down * PresetScript.moveSpeed * Time.deltaTime, Space.World); } if (Input.GetKey(KeyCode.A)) { transform.Translate(Vector2.left * PresetScript.moveSpeed * Time.deltaTime, Space.World); } if (Input.GetKey(KeyCode.D)) { transform.Translate(Vector2.right * PresetScript.moveSpeed * Time.deltaTime, Space.World); } // Attack if (Input.GetMouseButtonDown(0)) { StartCoroutine(PresetScript.Attack()); } // LongAttack if (Input.GetMouseButtonDown(1)) { StartCoroutine(PresetScript.LongAttack()); } // Heal if (Input.GetKeyDown(KeyCode.E)) { StartCoroutine(PresetScript.Heal()); } }
// Update is called once per frame void Update() { // Find the nearest enemy float distanceThreshold = 1000; foreach (GameObject person in AllChar) { if (person.activeInHierarchy) { if (person.GetComponent <CharacterPreset>().Team != gameObject.GetComponent <CharacterPreset>().Team) { float distance = Vector3.Distance(transform.localPosition, person.transform.localPosition); if (distance < distanceThreshold) { distanceThreshold = distance; Nearest = person; } } } } // Attack when approaching enemy if (distanceThreshold <= attackDistance) { if (!GetComponentInChildren <Weapon>().Attacking) { StartCoroutine(PresetScript.Attack()); } else { transform.Translate(Vector2.down * PresetScript.moveSpeed * Time.deltaTime, Space.Self); } } else { // Rotate to the position of enemy Vector2 mypos2 = new Vector2(Camera.main.WorldToScreenPoint(gameObject.transform.position).x, Camera.main.WorldToScreenPoint(gameObject.transform.position).y); Vector2 targetpos2 = new Vector2(Camera.main.WorldToScreenPoint(Nearest.transform.position).x, Camera.main.WorldToScreenPoint(Nearest.transform.position).y); float angle = Vector2.SignedAngle(new Vector2(0, 1), targetpos2 - mypos2); transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle)); if (collideOnBlock) { if ((angle - angleToBlock) > -60) { // Turn left if collide on block transform.Translate(Vector2.left * PresetScript.moveSpeed * Time.deltaTime, Space.World); } else if ((angle - angleToBlock) < 60) { // Turn right if collide on block transform.Translate(Vector2.right * PresetScript.moveSpeed * Time.deltaTime, Space.World); } else { // Move to the position of enemy transform.Translate(Vector2.up * PresetScript.moveSpeed * Time.deltaTime, Space.Self); } } else { // Move to the position of enemy transform.Translate(Vector2.up * PresetScript.moveSpeed * Time.deltaTime, Space.Self); } } if (PresetScript.HealthPoint <= 100) { StartCoroutine(PresetScript.Heal()); } }