我创建了FPS运动,但玩家的运动是错误的。左右旋转时,玩家仍会向前移动。
PlayerMovement.cs
public class PlayerMovement : MonoBehaviour
{
private Vector3 movement;
private Rigidbody rigid;
private bool jumpPressed;
private const int MOVEMENT_SPEED = 8;
private const int JUMP_POWER = 20;
private void Awake()
{
rigid = GetComponent<Rigidbody>();
}
private void Update()
{
SetInputs();
}
private void FixedUpdate()
{
Jump();
Move();
}
private void SetInputs()
{
movement.x = Input.GetAxis("Horizontal") * MOVEMENT_SPEED;
movement.y = rigid.velocity.y;
movement.z = Input.GetAxis("Vertical") * MOVEMENT_SPEED;
jumpPressed = Input.GetKeyDown(KeyCode.Space);
}
private void Jump()
{
if (jumpPressed && GroundCheck())
{
movement.y = JUMP_POWER;
jumpPressed = false;
}
}
private void Move()
{
rigid.velocity = movement;
}
private bool GroundCheck()
{
return true;
}
}
CameraMovement.cs
public class CameraMovement : MonoBehaviour
{
private Transform player;
private Vector2 rotation;
private Quaternion originalRotation;
private const int HORIZONTAL_ROTATION_SPEED = 5;
private const int VERTICAL_ROTATION_SPEED = 5;
private const int VERTICAL_ROTATION_MIN = -80;
private const int VERTICAL_ROTATION_MAX = 80;
private void Awake()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
}
private void Start()
{
originalRotation = transform.localRotation;
}
private void Update()
{
SetInputs();
RotateCamera();
RotatePlayer();
}
private void SetInputs()
{
rotation.x = Input.GetAxisRaw("Mouse X") * HORIZONTAL_ROTATION_SPEED;
rotation.y += Input.GetAxisRaw("Mouse Y") * VERTICAL_ROTATION_SPEED;
rotation.y = Mathf.Clamp(rotation.y, VERTICAL_ROTATION_MIN, VERTICAL_ROTATION_MAX);
}
private void RotateCamera()
{
Quaternion verticalRotation = Quaternion.AngleAxis(rotation.y, Vector3.left);
transform.localRotation = originalRotation * verticalRotation;
}
private void RotatePlayer()
{
player.localRotation *= Quaternion.AngleAxis(rotation.x, player.up);
}
}
我提供了一个小的gif,显示了旋转时的错误运动。
https://media.giphy.com/media/2jMy38g1PjckODc6B0/giphy.gif
通过旋转相机旋转播放器时,播放器会绕其y轴正确旋转。不知何故,他没有朝自己面对的方向前进,只是沿一个方向前进。
需要解决什么?
答案 0 :(得分:0)
我看到您的移动逻辑与相机朝向无关,它只是使对象随输入轴值移动,这就是为什么您的对象像这样移动。
如果要使“前进”轴表示“向我的朝向前进”,则应将移动与相机旋转结合起来。
您可以尝试类似
client=boto3.client('s3')
在您的Move()中。
答案 1 :(得分:0)
这是在重新发明轮子。统一的标准资产套件随附可自定义且效果良好的fps预制件。该脚本功能强大,控件优雅,只需稍作调整即可轻松制作自己的脚本,节省一些时间,从unity store下载Unity Standard Asset
软件包,并使用firstperson char prefab。我想你会很高兴的。