0906
This commit is contained in:
1 parent
5cd7a93c4a
commit
1476a089cb
108 files changed
+32073
-117261
No files matched your search
@@ -0,0 +1,120 @@
|
||||
using UnityEngine;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user