帮我写个unity人物移动及视角转动脚本
时间: 2023-03-29 14:02:01 浏览: 176
Unity3D实现鼠标控制视角转动
5星 · 资源好评率100%
好的,我可以回答这个问题。以下是一个简单的Unity人物移动及视角转动脚本:
```
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
public float lookSensitivity = 3f;
private Camera playerCamera;
private Rigidbody playerRigidbody;
private float verticalLookRotation;
void Start()
{
playerCamera = Camera.main;
playerRigidbody = GetComponent<Rigidbody>();
}
void Update()
{
// 移动
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 moveDirection = new Vector3(horizontal, f, vertical).normalized;
Vector3 moveVelocity = moveDirection * moveSpeed;
playerRigidbody.MovePosition(playerRigidbody.position + moveVelocity * Time.deltaTime);
// 视角转动
float mouseX = Input.GetAxis("Mouse X") * lookSensitivity;
float mouseY = Input.GetAxis("Mouse Y") * lookSensitivity;
verticalLookRotation += mouseY;
verticalLookRotation = Mathf.Clamp(verticalLookRotation, -90f, 90f);
playerCamera.transform.localEulerAngles = new Vector3(-verticalLookRotation, f, f);
transform.eulerAngles += Vector3.up * mouseX;
}
}
```
这个脚本可以让玩家使用 WASD 键移动人物,使用鼠标控制视角转动。希望对你有帮助!
阅读全文