using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using Cinemachine;
public class Rocker : MonoBehaviour, IEndDragHandler, IBeginDragHandler, IDragHandler
{
public CinemachineVirtualCamera camera1;
public Vector2 ve, start2;//声明两个变量
public bool a = false;//检测控制鼠标是否工作
public GameObject cube;//控制的对象
public void OnBeginDrag(PointerEventData eventData)
{
start2 = transform.position;//先获取对象位置
a = true;//检查鼠标是否按下
}
public void OnDrag(PointerEventData eventData)
{
transform.position = Input.mousePosition;//让拖拽对象跟着鼠标跑
ve = eventData.position - start2;//计算距离差值(原来位置的和移动的位置的差值)
transform.localPosition = Vector2.ClampMagnitude(ve, 50f);//限制拖拽每次不能超过30(限制对象ve)
}
public void OnEndDrag(PointerEventData eventData)
{
a = false;//鼠标离开操作
transform.localPosition = Vector2.zero;//还原图片位置到黑色图片中心位置
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (a)//如果检测到发生改变
{
cube.transform.LookAt(cube.transform.position + new Vector3(ve.x, 0, ve.y));//被控制的对象除高以外改变方向
cube.transform.Translate(0, 0, Time.deltaTime * 5f);//触发移动(自动移动以时间为单位向前移动)
cube.transform.GetComponent<Animator>().SetBool("run",true);
}
else
{
cube.transform.GetComponent<Animator>().SetBool("run", false);
}
}
}
手写摇杆移动
最新推荐文章于 2024-07-25 00:09:37 发布