Files
daluandou/Assets/Script/TurnAroundTest.cs
T
2026-08-09 23:42:37 +08:00

42 lines
988 B
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TurnArouundTest : MonoBehaviour
{
public float moveSpeed = 1f; // 移动速度
void Start()
{
}
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);
}
}
}
}