手写摇杆移动

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);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值