EasyTouch

EasyTouch

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class EnenyTouchMove : MonoBehaviour,IDragHandler
{
    Vector2 position;//圆心初始化位置
    public float radios = 200;//圆的半径
    public GameObject target;//目标
    public GameObject DownImage;
    public float speed;
      
    public void OnDrag(PointerEventData eventData)
    {
        Vector2 deltaPos = eventData.position - (Vector2)DownImage.transform.position;
        if (deltaPos.magnitude < radios)
        {
            transform.position = eventData.position;
        }
        else
        {
            transform.position = (Vector2)DownImage.transform.position + deltaPos.normalized * radios;
            //求角度        反正切得到弧度
            float myAngle = Mathf.Atan2(deltaPos.y, deltaPos.x) * Mathf.Rad2Deg;
            if (target != null)
            {
                Vector3 tmpAngle = target.transform.localEulerAngles;
                tmpAngle.y = 90 - myAngle;
                target.transform.localEulerAngles = tmpAngle;
            }
        }
    }


    // Start is called before the first frame update
    void Start()
    {
        position = transform.position;

    }

    // Update is called once per frame
    void Update()
    {
        DownImage.transform.position = Vector3.Lerp(DownImage.transform.position, transform.position, speed*Time.deltaTime);
    }
}

  

posted @ 2021-02-26 15:03  一路繁华的夏ˇ  阅读(20)  评论(0)    收藏  举报