我目前正在用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);
}
}
以下是播放器附带的相关组件:
(播放器还附加了几个组件,但我在没有它们的情况下进行了测试,但问题仍然存在)
就像我说我是一个团结的新手一样,所以我确定我错过了一些小东西,但是我似乎无法将手指放在上面,我已经坚持了一段时间任何帮助深表感谢。
答案 0 :(得分:3)
已解决:
看来我遇到的问题是因为我是从笔记本电脑而不是台式机上运行场景,我认为这是为Unity输入构建的。