123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerControl : MonoBehaviour
- {
- public float minSpeed = 0.7f;
- public float maxSpeed = 1.4f;
- private float dis = 0;
- private float player_speed = 0;
- // Use this for initialization
- void Start()
- {
- }
- // Update is called once per frame
- void Update()
- {
- Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- mousePos.z = 0;
- dis = Vector2.Distance(new Vector2(transform.position.x, transform.position.y), new Vector2(mousePos.x, mousePos.y));
- if (dis <= 0.1f)
- {
- }
- else
- {
- TurnTo(mousePos);
- player_speed = dis * 0.5f;
- player_speed = Mathf.Clamp(player_speed, minSpeed, maxSpeed);
- transform.Translate(Vector3.up * Time.deltaTime * player_speed);
- }
- }
- void TurnTo(Vector3 pos)
- {
- Vector3 dir = pos - transform.position;
- float theta = Mathf.Atan2(dir.y, dir.x);
- float angle = theta * Mathf.Rad2Deg;
- transform.localEulerAngles = new Vector3(0, 0, angle - 90);
- }
- void OnTriggerEnter2D(Collider2D other)
- {
- Destroy(other.gameObject);
- }
- }
|