gpt4 book ai didi

c# - 不能同时旋​​转和移动

转载 作者:行者123 更新时间:2023-12-02 02:01:36 24 4
gpt4 key购买 nike

我目前正在 Unity 3D 中开发一款 FPS 射击游戏。我对 Unity 还很陌生,而且我的玩家移动脚本遇到了一些问题。单独来看,一切似乎都正常,我可以自由旋转和移动播放器,但是当我尝试同时执行这两个操作时,我的播放器似乎锁定并且不会旋转。

有时跳跃或移动到地形上较高的地面似乎可以解决问题,但是我在禁用重力、没有碰撞器且玩家位于地面上方的情况下运行了一些检查,因此问题似乎出在脚本上。我还做了一些调试,Rotate() 代码确实运行了,只是旋转量似乎没有改变。

这是玩家移动脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class PlayerMov : MonoBehaviour
{
[SerializeField]
private Camera cam;
[SerializeField]
private float speed = 5f;
[SerializeField]
private float looksensitivity = 4f;

[Header("Camera View Lock:")]
[SerializeField] //min and max amount for looking up and down (degrees)
private float lowlock = 70f;
[SerializeField]
private float highlock = 85f;

private Rigidbody rb;
private float currentrotx; //used later for calculating relative rotation
public Vector3 velocity;
public Vector3 rotation; // rotates the player from side to side
public float camrotation; // rotates the camera up and down
public float jmpspe = 2000f;

void Start()
{
rb = GetComponent<Rigidbody>();
}

// Update is called once per frame
void Update()
{
CalcMovement();

CalcRotation();

Rotate();
}

void FixedUpdate()
{
Move();
}

private void CalcRotation()
{
float yrot = Input.GetAxisRaw("Mouse X"); //around x axis
rotation = new Vector3(0f, yrot, 0f) * looksensitivity;

float xrot = Input.GetAxisRaw("Mouse Y"); //around y axis
camrotation = xrot * looksensitivity;
}

private void CalcMovement()
{
float xmov = Input.GetAxisRaw("Horizontal");
float zmov = Input.GetAxisRaw("Vertical");

Vector3 movhor = transform.right * xmov;
Vector3 movver = transform.forward * zmov;

velocity = (movhor + movver).normalized * speed;
}

void Move()
{
//move
if (velocity != Vector3.zero)
{
rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
}

//jump
if (Input.GetKeyDown(KeyCode.Space))
{
//add double jump limit later!
rb.AddForce(0, jmpspe * Time.deltaTime, 0, ForceMode.Impulse);

}
}

void Rotate()
{
//looking side to side
rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation));

// camera looking up and down
currentrotx -= camrotation;
currentrotx = Mathf.Clamp(currentrotx, -lowlock, highlock);
cam.transform.localEulerAngles = new Vector3(currentrotx, 0, 0);
}
}

以下是连接到我的播放器的相关组件:

enter image description here

(播放器还附加了几个组件,但我在没有它们的情况下进行了测试,问题仍然出现)

就像我说的,我是一个团结新手,所以我确信我错过了一些小东西,但我似乎无法将手指放在它上面,我已经坚持了一段时间了,所以非常感谢任何帮助。

最佳答案

已解决:

我遇到的问题似乎是因为我是从笔记本电脑运行场景,而不是在台式机上运行场景,我认为这是 Unity 输入的用途。

关于c# - 不能同时旋​​转和移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59478453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com