如果你想让角色在移动时播放行走动画,可以使用 Animator 组件来实现。以下是修改后的代码:
using UnityEngine;
using System.Collections;
public class PlayerController1 : MonoBehaviour
{
public GameObject circlePrefab;
private GameObject currentCircle;
private Vector3 targetPosition;
private bool facingRight = true;
// 新增变量:限制角色移动范围
public Rect moveRange;
Coroutine currentCoroutine;
// 新增变量:Animator 组件
private Animator animator;
void Start()
{
// 获取角色上的 Animator 组件
animator = GetComponent<Animator>();
// 初始状态为闲置动画
animator.SetBool("IsWalking", false);
}
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;
currentCircle.transform.position = touchPos;
}
break;
case TouchPhase.Ended:
if (currentCircle != null)
{
HandleTouchEndedOrCanceled(touch);
Destroy(currentCircle);
}
break;
case TouchPhase.Canceled:
break;
}
}
// 检查当前是否在移动中
bool isMoving = currentCoroutine != null && Vector3.Distance(transform.position, targetPosition) > 0.01f;
animator.SetBool("IsWalking", isMoving);
}
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);
// 新增限制:将目标位置限制在 moveRange 范围内
targetPosition.x = Mathf.Clamp(targetPos.x, moveRange.xMin, moveRange.xMax);
targetPosition.y = Mathf.Clamp(targetPos.y, moveRange.yMin, moveRange.yMax);
if (currentCoroutine != null)
StopCoroutine(currentCoroutine);
currentCoroutine = StartCoroutine(MoveToTarget());
}
}
IEnumerator MoveToTarget()
{
float moveSpeed = 2.5f;
float bufferDistance = 0.01f;
Vector3 targetDirection;
while (Vector3.Distance(transform.position, targetPosition) > bufferDistance)
{
if (transform.position == targetPosition)
yield break;
// 新增限制:检查角色是否超出了移动范围
Vector3 clampedPos = transform.position;
clampedPos.x = Mathf.Clamp(clampedPos.x, moveRange.xMin, moveRange.xMax);
clampedPos.y = Mathf.Clamp(clampedPos.y, moveRange.yMin, moveRange.yMax);
transform.position = clampedPos;
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;
}
}
现在,当角色开始移动时,Animator 组件会自动切换为行走动画,并在停止移动后切换回闲置动画。