56 lines
1.5 KiB
C#
56 lines
1.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class AnimatedCharacterMovement : MonoBehaviour
|
|
{
|
|
public float moveSpeed = 1f; // 移动速度
|
|
private Animator animator;
|
|
private CharacterController controller;
|
|
|
|
void Start()
|
|
{
|
|
animator = GetComponent<Animator>();
|
|
controller = GetComponent<CharacterController>();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
|
|
// 获取点击输入
|
|
if (Input.GetMouseButton(0))
|
|
{
|
|
// 获取点击位置
|
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
|
RaycastHit hit;
|
|
|
|
// 检测点击位置是否有物体
|
|
if (Physics.Raycast(ray, out hit))
|
|
{
|
|
// 获取点击位置的坐标
|
|
Vector3 targetPosition = hit.point;
|
|
|
|
// 计算移动方向
|
|
Vector3 movement = targetPosition - transform.position;
|
|
movement.y = 0; // 确保只在水平面上旋转
|
|
|
|
//改变角色朝向
|
|
transform.rotation = Quaternion.LookRotation(movement, Vector3.up);
|
|
|
|
//改变动画控制器的参数
|
|
animator.SetInteger("speed", 1);
|
|
|
|
//角色像点击处移动
|
|
//获取movement向量
|
|
Vector3 move = movement.normalized;
|
|
controller.Move(move * moveSpeed * Time.deltaTime);
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
animator.SetInteger("speed", 0);
|
|
}
|
|
}
|
|
}
|