PlayerControl.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlayerControl : MonoBehaviour
  5. {
  6. public float minSpeed = 0.7f;
  7. public float maxSpeed = 1.4f;
  8. private float dis = 0;
  9. private float player_speed = 0;
  10. // Use this for initialization
  11. void Start()
  12. {
  13. }
  14. // Update is called once per frame
  15. void Update()
  16. {
  17. Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  18. mousePos.z = 0;
  19. dis = Vector2.Distance(new Vector2(transform.position.x, transform.position.y), new Vector2(mousePos.x, mousePos.y));
  20. if (dis <= 0.1f)
  21. {
  22. }
  23. else
  24. {
  25. TurnTo(mousePos);
  26. player_speed = dis * 0.5f;
  27. player_speed = Mathf.Clamp(player_speed, minSpeed, maxSpeed);
  28. transform.Translate(Vector3.up * Time.deltaTime * player_speed);
  29. }
  30. }
  31. void TurnTo(Vector3 pos)
  32. {
  33. Vector3 dir = pos - transform.position;
  34. float theta = Mathf.Atan2(dir.y, dir.x);
  35. float angle = theta * Mathf.Rad2Deg;
  36. transform.localEulerAngles = new Vector3(0, 0, angle - 90);
  37. }
  38. void OnTriggerEnter2D(Collider2D other)
  39. {
  40. Destroy(other.gameObject);
  41. }
  42. }