Files
daluandou/Assets/Script/MainCharacterDebugMovement.cs
T
huadexuan 2dfffd57fe ```
feat(gameplay): 更新核心场景中的卡牌和能量豆配置

- 将能量豆5重命名为能量豆6,能量豆4重命名为能量豆7等,调整了所有
  能量豆的命名顺序
- 将卡牌5重命名为卡牌1,并添加了新的组件包括召唤槽脚本和按钮交互
  组件
- 为卡牌添加子对象Image用于显示卡牌内容
- 调整了卡牌和能量豆的层级结构和父子关系
- 实现了通过数字键1和2进行召唤的功能,支持指定召唤槽位
- 添加了UI元素的射线检测控制,部分元素设置为不响应射线点击

fix(input): 防止在UI元素上移动角色

- 在移动输入检查中增加了对EventSystem的判断,当鼠标指针位于
  UI元素上方时禁止角色移动
- 避免了玩家在操作UI时意外移动角色的问题
```
2026-09-08 23:53:24 +08:00

126 lines
3.0 KiB
C#

using UnityEngine;
using UnityEngine.EventSystems;
public class MainCharacterDebugMovement : MonoBehaviour
{
[Header("电脑调试移动")]
[Min(0f)]
public float moveSpeed = 4f;
[Min(0f)]
public float clickStopDistance = 0.08f;
[Header("移动范围")]
public bool limitToBattlePlatform = true;
public Vector2 xLimits = new Vector2(-7.5f, 7.5f);
public Vector2 zLimits = new Vector2(-14f, 14f);
private Animator animator;
private Vector3 clickTarget;
private bool hasClickTarget;
public bool IsMoving =>
hasClickTarget ||
new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")).sqrMagnitude > 0.001f;
private void Start()
{
animator = GetComponent<Animator>();
clickTarget = transform.position;
SetWalking(false);
}
private void Update()
{
HandleClickTarget();
Vector3 keyboardInput = new Vector3(
Input.GetAxisRaw("Horizontal"),
0f,
Input.GetAxisRaw("Vertical"));
if (keyboardInput.sqrMagnitude > 0.001f)
{
hasClickTarget = false;
Move(keyboardInput.normalized);
return;
}
if (!hasClickTarget)
{
SetWalking(false);
return;
}
Vector3 toTarget = clickTarget - transform.position;
toTarget.y = 0f;
if (toTarget.magnitude <= clickStopDistance)
{
hasClickTarget = false;
SetWalking(false);
return;
}
Move(toTarget.normalized);
}
private void HandleClickTarget()
{
if (!Input.GetMouseButtonDown(0) || Camera.main == null)
{
return;
}
if (EventSystem.current != null && EventSystem.current.IsPointerOverGameObject())
{
return;
}
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (!Physics.Raycast(ray, out RaycastHit hit))
{
return;
}
clickTarget = hit.point;
clickTarget.y = transform.position.y;
hasClickTarget = true;
}
public void StopMovement()
{
hasClickTarget = false;
SetWalking(false);
}
private void Move(Vector3 direction)
{
direction.y = 0f;
if (direction.sqrMagnitude <= 0.001f)
{
SetWalking(false);
return;
}
SetWalking(true);
transform.position += direction * moveSpeed * Time.deltaTime;
if (limitToBattlePlatform)
{
Vector3 position = transform.position;
position.x = Mathf.Clamp(position.x, xLimits.x, xLimits.y);
position.z = Mathf.Clamp(position.z, zLimits.x, zLimits.y);
transform.position = position;
}
transform.rotation = Quaternion.LookRotation(direction, Vector3.up);
}
private void SetWalking(bool walking)
{
if (animator != null)
{
animator.SetInteger("speed", walking ? 1 : 0);
}
}
}