我想创建FPS游戏,玩家可以移动W A S D和空格键跳跃,但也可以在游戏中心鼠标,我可以旋转相机,当我移动我的相机也需要我检查冻结旋转 * 所有游戏都是三维的
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class PlayerController : MonoBehaviour
{
public float speed = 1.5f;
public Transform head;
public float sensitivity = 5f;
public float headMinY = -40f;
public float headMaxY = 40f;
public KeyCode jumpButton = KeyCode.Space;
public float jumpForce = 10;
public float jumpDistance = 1.2f;
private Vector3 direction;
private float h, v;
private int layerMask;
private Rigidbody body;
private float rotationY;
void Start()
{
body = GetComponent<Rigidbody>();
body.freezeRotation = true;
layerMask = 1 << gameObject.layer | 1 << 2;
layerMask = ~layerMask;
}
void FixedUpdate()
{
body.AddForce(direction * speed, ForceMode.VelocityChange);
// Ограничение скорости, иначе объект будет постоянно ускоряться
if (Mathf.Abs(body.velocity.x) > speed)
{
body.velocity = new Vector3(Mathf.Sign(body.velocity.x) * speed, body.velocity.y, body.velocity.z);
}
if (Mathf.Abs(body.velocity.z) > speed)
{
body.velocity = new Vector3(body.velocity.x, body.velocity.y, Mathf.Sign(body.velocity.z) * speed);
}
}
bool GetJump()
{
RaycastHit hit;
Ray ray = new Ray(transform.position, Vector3.down);
if (Physics.Raycast(ray, out hit, jumpDistance, layerMask))
{
return true;
}
return false;
}
void Update()
{
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
float rotationX = head.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivity;
rotationY += Input.GetAxis("Mouse Y") * sensitivity;
rotationY = Mathf.Clamp(rotationY, headMinY, headMaxY);
head.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
direction = new Vector3(h, 0, v);
direction = head.TransformDirection(direction);
direction = new Vector3(direction.x, 0, direction.z);
if (Input.GetKeyDown(jumpButton) && GetJump())
{
body.velocity = new Vector2(0, jumpForce);
}
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawRay(transform.position, Vector3.down * jumpDistance);
}
}
1条答案
按热度按时间s4chpxco1#
您的代码看起来基本正确。但是,您需要将
fixUpdate
替换为update
,以便在更新后更平滑地处理输入,因为它每帧调用一次。其次,您需要更改GetJump()方法中的body.velocity
赋值,应更改为body.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
看看能不能成功