作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在 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);
}
}
以下是连接到我的播放器的相关组件:
(播放器还附加了几个组件,但我在没有它们的情况下进行了测试,问题仍然出现)
就像我说的,我是一个团结新手,所以我确信我错过了一些小东西,但我似乎无法将手指放在它上面,我已经坚持了一段时间了,所以非常感谢任何帮助。
最佳答案
已解决:
我遇到的问题似乎是因为我是从笔记本电脑运行场景,而不是在台式机上运行场景,我认为这是 Unity 输入的用途。
关于c# - 不能同时旋转和移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59478453/
我是一名优秀的程序员,十分优秀!