Unity 使用UGUI制作摇杆控制角色移动

准备:

创建场景,通过UGUI创建两个Image,分别作为摇杆及其背景。

在场景中加入角色模型,作为控制的对象(Player),设置角色控制器Animator。

示例:

 

Idle--->Run

Run---Idle

为Image_YG即摇杆添加脚本EasyTouchMove

为Player即角色添加脚本PlayerControl

脚本如下:

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

public class EasyTouchMove : MonoBehaviour, IDragHandler, IEndDragHandler
{
    //图标移动最大半径
    public float maxRadius = 100;
    //初始化背景图标位置
    private Vector2 moveBackPos;

    //hor,ver的属性访问器
    private float horizontal = 0;
    private float vertical = 0;

    public float Horizontal
    {
        get { return horizontal; }
    }

    public float Vertical
    {
        get { return vertical; }
    }

    void Start()
    {
        //初始化背景图标位置
        moveBackPos = transform.parent.transform.position;
    }

    void Update()
    {
        horizontal = transform.localPosition.x;
        vertical = transform.localPosition.y;
    }

    /// <summary>
    /// 当鼠标开始拖拽时
    /// </summary>
    /// <param name="eventData"></param>
    public void OnDrag(PointerEventData eventData)
    {
        //获取鼠标位置与初始位置之间的向量
        Vector2 oppsitionVec = eventData.position - moveBackPos;
        //获取向量的长度
        float distance = Vector3.Magnitude(oppsitionVec);
        //最小值与最大值之间取半径
        float radius = Mathf.Clamp(distance, 0, maxRadius);
        //限制半径长度
        transform.position = moveBackPos + oppsitionVec.normalized * radius;
    }

    /// <summary>
    /// 当鼠标停止拖拽时
    /// </summary>
    /// <param name="eventData"></param>
    public void OnEndDrag(PointerEventData eventData)
    {
        transform.position = moveBackPos;
        transform.localPosition = Vector3.zero;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControl : MonoBehaviour
{
    //获取遥感脚本
    public EasyTouchMove touch;
    //动画控制器
    Animator ani;

    void Start()
    {
        ani = gameObject.GetComponent<Animator>();//获取组件
    }

    void Update()
    {
        //获取horizontal 和 vertical 的值,其值位遥感的localPosition
        float hor = touch.Horizontal;
        float ver = touch.Vertical;

        Vector3 direction = new Vector3(hor, 0, ver);

        if (direction != Vector3.zero)
        {
            //控制移动
            float newSpeed = Mathf.Lerp(ani.GetFloat("Action"), 5, Time.deltaTime * 5);
            //播放动画
            ani.SetFloat("Action", newSpeed);
            //控制旋转
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(direction), Time.deltaTime * 10);
            //向前移动
            transform.Translate(Vector3.forward * Time.deltaTime * newSpeed);
        }
        else
        {
            //停止移动
            ani.SetFloat("Action", 0);
        }
    }
}

运行前,将绑定了EasyTouchMove脚本的Image_YG添加到Player物体上添加的PlayerControl脚本的Touch里。

运行即可。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值