不能同时旋转和移动
Can't rotate and move at the same time
我目前正在 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 输入的目标的台式机。
我目前正在 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 输入的目标的台式机。