ZBLOG

using UnityEngine; using System.Collections; public class PlayerController1 : MonoBehaviour { public GameObject circlePrefab; //光圈预制体,需要提前准备好 private GameObject currentCircle; //当前生成的光圈 private Vector3 targetPos; //目标位置 private boo...

using UnityEngine;
using System.Collections;

public class PlayerController1 : MonoBehaviour
{
    public GameObject circlePrefab; //光圈预制体,需要提前准备好
    private GameObject currentCircle; //当前生成的光圈
    private Vector3 targetPos; //目标位置
    private bool facingRight = true; // 是否面向右侧

    Coroutine currentCoroutine;

    void Update()
    {
        if (Input.touchCount > 0) //检测是否有触摸事件发生
        {
            Touch touch = Input.GetTouch(0); //获取第一个触点的信息

            switch (touch.phase)
            {
                case TouchPhase.Began: //触摸开始时生成光圈并将其放到角色脚下
                    currentCircle = Instantiate(circlePrefab, transform.position, Quaternion.identity);
                    break;

                case TouchPhase.Moved: //移动时在触摸位置生成另一个光圈(不移动角色)
                    if (currentCircle != null)
                    {
                        Vector3 touchPos = Camera.main.ScreenToWorldPoint(touch.position); //将屏幕坐标转换为世界坐标
                        touchPos.z = 0;
                        currentCircle.transform.position = touchPos;
                    }
                    break;

                case TouchPhase.Ended: //触摸结束时设置目标位置,并销毁当前光圈,播放跑步动画
                    if (currentCircle != null)
                        Destroy(currentCircle);

                    targetPos = Camera.main.ScreenToWorldPoint(touch.position); //将屏幕坐标转换为世界坐标,作为目标位置
                    targetPos.z = 0;

                    if (currentCoroutine != null) //如果当前有正在执行的协程,则停止之前的协程
                        StopCoroutine(currentCoroutine);

                    currentCoroutine = StartCoroutine(MoveToTarget()); //启动协程执行移动操作,并将其记录到变量中以便后续操作
                    break;

                case TouchPhase.Canceled: //触摸取消时销毁当前光圈,开始播放跑步动画直到角色移动到目标位置
                    if (currentCircle != null)
                        Destroy(currentCircle);

                    if (currentCoroutine != null) //如果当前有正在执行的协程,则停止之前的协程
                        StopCoroutine(currentCoroutine);

                    currentCoroutine = StartCoroutine(MoveToTarget()); //启动协程执行移动操作,并将其记录到变量中以便后续操作
                    break;
            }
        }
    }

    IEnumerator MoveToTarget()
    {
        float moveSpeed = 5f; // 固定速度为5米/秒
        float bufferTime = 0.1f; // 缓冲时间为0.5秒
        Vector3 targetDir;

        while (Vector3.Distance(transform.position, targetPos) > 0.1f)
        {
            targetDir = (targetPos - transform.position).normalized; // 计算目标方向
            if (targetDir.x > 0 && !facingRight) // 目标在右侧且当前未面向右侧
            {
                Flip(); // 翻转角色朝向,使其面向右侧
            }
            else if (targetDir.x < 0 && facingRight) // 目标在左侧且当前面向右侧
            {
                Flip(); // 翻转角色朝向,使其面向左侧
            }

            if (Vector3.Distance(transform.position, targetPos) < bufferTime * moveSpeed) // 如果距离小于缓冲时间的移动距离,则认为已经到达目标点
            {
                transform.position = targetPos; // 直接移动到目标位置
                yield break; // 终止协程
            }

            transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime); // 每帧移动一小段距离

            yield return null;
        }
    }

    void Flip()
    {
        facingRight = !facingRight; // 修改状态变量,记录当前是否面向右侧

        Vector3 scale = transform.localScale;
        scale.x *= -1; // 翻转 x 方向上的缩放值
        transform.localScale = scale; // 更新缩放值,实现翻转效果
    }
}

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://sdn.0voice.com/?id=1101

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?