我是跟随韦尔顿国王的教程如何制作fps,我是在第三个视频,我写了这个。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Com.Pat12.FPS
{
public class Motion : MonoBehaviour
{
public float speed;
public float sprintModifier;
public float jumpForce;
public Camera normalCam;
public Transform groundDetector;
public LayerMask ground;
private Rigidbody rig;
private float baseFOV;
private float sprintFOVModifier = 1.5f;
private void Start()
{
baseFOV = normalCam.fieldOfView;
Camera.main.enabled = false;
rig = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
//Axis
float t_hmove = Input.GetAxisRaw("Horizontal");
float t_vmove = Input.GetAxisRaw("Vertical");
//Controls
bool sprint = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
bool jump = Input.GetKeyDown(KeyCode.Space);
//States
bool isGrounded = Physics.Raycast(groundDetector.position, Vector3.down, 0.1f, ground);
bool isJumping = jump && isGrounded;
bool isSprinting = sprint && t_vmove > 0 && !isJumping && isGrounded;
//Jumping
if (isJumping)
{
rig.AddForce(Vector3.up * jumpForce);
}
//Movement
Vector3 t_direction = new Vector3(t_hmove, 0, t_vmove);
t_direction.Normalize();
float t_adjustedSpeed = speed;
if (isSprinting) t_adjustedSpeed *= sprintModifier;
Vector3 t_targetVelocity = t_direction * t_adjustedSpeed * Time.deltaTime;
t_targetVelocity.y = rig.velocity.y;
rig.velocity = t_targetVelocity;
//FOV
if (isSprinting)
{
normalCam.fieldOfView = Mathf.Lerp(normalCam.fieldOfView, baseFOV * sprintFOVModifier, Time.deltaTime * 8f);
}
else
{
normalCam.fieldOfView = Mathf.Lerp(normalCam.fieldOfView, baseFOV, Time.deltaTime * 8f);
}
}
}
}问题是当我把我的球员转过来的时候,w键是不会前进的。它只是按地面移动,而不管它的脸如何。
发布于 2022-03-17 13:46:29
修好了。你必须让它成为transform.TransformDirection(t_direction )
https://stackoverflow.com/questions/71512167
复制相似问题