ZBLOG

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

好的,修改后的代码如下:

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:
                    if (IsTouchedOnPlayer(touch.position)) //如果触摸到角色,则在角色脚下生成光圈,并返回,不进行移动操作
                        return;
                    else
                        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:
                case TouchPhase.Canceled: 
                    if (currentCircle != null)
                        Destroy(currentCircle);

                    Collider2D col = GetComponent<Collider2D>();
                    Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
                    if (col == Physics2D.OverlapPoint(touchPos)) //如果结束时还在角色身上,则直接返回,不进行移动操作
                        return;

                    targetPos = touchPos; //将屏幕坐标转换为世界坐标,作为目标位置
                    targetPos.z = 0;

                    if (currentCoroutine != null)
                        StopCoroutine(currentCoroutine);

                    currentCoroutine = StartCoroutine(MoveToTarget());
                    break;
            }
        }
    }

    bool IsTouchedOnPlayer(Vector2 touchPos)
    {
        Collider2D col = GetComponent<Collider2D>();
        return col == Physics2D.OverlapPoint(touchPos); //如果触摸点在角色collider范围内,则认为触摸在角色身上
    }

    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;
        transform.localScale = scale;
    }
}

需要注意的是,修改判断条件后,不再需要使用Vector2.Distance函数计算距离。同时,在End和Cancel状态下,都需要进行角色collider2d的检测。

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?