ZBLOG

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

using UnityEngine; using System.Collections;

public class PlayerController1 : MonoBehaviour {

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

Coroutine currentCoroutine;

[Header("Movement Bounds")]
public float minX;
public float maxX;
public float minY;
public float maxY;

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

        switch (touch.phase)
        {
            case TouchPhase.Began:
                if (IsTouchedOnPlayer(touch.position)) // 如果触摸到角色,则在角色脚下生成光圈,并返回,不进行移动操作
                {
                    currentCircle = Instantiate(circlePrefab, transform.position, Quaternion.identity);
                    return;
                }
                break;

            case TouchPhase.Moved:
                if (currentCircle != null) // 如果有光圈,则在手指移动的位置生成另外一个光圈(不移动角色)
                {
                    Vector3 touchPos = Camera.main.ScreenToWorldPoint(touch.position); // 将屏幕坐标转换为世界坐标
                    touchPos.z = 0;
                    touchPos.x = Mathf.Clamp(touchPos.x, minX, maxX);
                    touchPos.y = Mathf.Clamp(touchPos.y, minY, maxY);
                    currentCircle.transform.position = touchPos;
                }
                break;

            case TouchPhase.Ended:
                if (currentCircle != null)
                {
                    HandleTouchEndedOrCanceled(touch);
                    Destroy(currentCircle);
                }
                break;
            case TouchPhase.Canceled:
                break;
        }
    }
}

private bool IsTouchedOnPlayer(Vector2 screenPosition)
{
    Vector2 worldPosition = Camera.main.ScreenToWorldPoint(screenPosition);
    Collider2D[] colliders = GetComponents<Collider2D>();
    foreach (var collider in colliders)
    {
        if (collider.OverlapPoint(worldPosition))
        {
            return true;
        }
    }
    return false;
}

void HandleTouchEndedOrCanceled(Touch touch)
{
    if (!IsTouchedOnPlayer(touch.position)) // 如果结束时没有在角色身上,则将屏幕坐标转换为世界坐标,作为目标位置
    {
        Vector2 targetPos = Camera.main.ScreenToWorldPoint(touch.position);
        targetPos.x = Mathf.Clamp(targetPos.x, minX, maxX);
        targetPos.y = Mathf.Clamp(targetPos.y, minY, maxY);
        targetPosition = targetPos;

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

        currentCoroutine = StartCoroutine(MoveToTarget());
    }
}

IEnumerator MoveToTarget()
{
    float moveSpeed = 2.5f; // 固定速度为5米/秒
    float bufferDistance = 0.01f; // 缓冲距离为0.1米
    Vector3 targetDirection;

    while (Vector3.Distance(transform.position, targetPosition) > bufferDistance)
    {
        if (transform.position == targetPosition) // 如果已经到达目标位置,则退出协程
            yield break;

        targetDirection = (targetPosition - transform.position).normalized; // 计算目标方向
        if (targetDirection.x > 0 && !facingRight)
        {
            Flip();
        }
        else if (targetDirection.x < 0 && facingRight)
        {
            Flip();
        }

        if (Vector3.Distance(transform.position, targetPosition) < moveSpeed * Time.deltaTime)
        {
            transform.position = targetPosition;
            yield break;
        }

        transform.position = Vector3.MoveTowards(transform.position, targetPosition, moveSpeed * Time.deltaTime);

        yield return null;
    }
}

void Flip()
{
    facingRight = !facingRight;

    Vector3 scale = transform.localScale;
    scale.x *= -1;
    transform.localScale = scale;
}

}

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?