Unity - 使用旋转相机更改鼠标控件
Unity - Change mouse controls with rotated camera
我目前正在研究零重力 fps 控件并且几乎所有的东西都按我想要的方式工作,但是我有一个问题,当我旋转角色和相机时,控件不会改变恰好适合。例如。宪章是水平的,但控件仍然像垂直一样。
有办法解决这个问题吗?这是我正在使用的代码。我对编码还很陌生,所以请多多关照。
public Rigidbody rb;
public Transform camerObj;
public Transform orientation;
public float camRotationSpeed = 5f;
public float cameraMinimumY = -60f;
public float cameraMaximumY = 75f;
public float rotationSmoothSpeed = 10f;
public float maxSpeed = 9f;
public float thrust = 20f;
public float forwardSwimSpeed = 45f;
private float xRotation;
private float sensitivity = 50f;
private float sensMultiplier = 1f;
void Update()
{
spaceRotation();
Look();
}
void FixedUpdate()
{
if (rb.velocity.magnitude > maxSpeed)
{
rb.velocity = rb.velocity.normalized * maxSpeed;
}
if (Input.GetKey(KeyCode.W))
{
rb.AddForce(camerObj.transform.TransformDirection(new Vector3(0, 0, 1) * thrust) * Time.deltaTime * forwardSwimSpeed, ForceMode.Acceleration);
}
if (Input.GetKey(KeyCode.S))
{
rb.AddForce(camerObj.transform.TransformDirection(new Vector3(0, 0, -1) * thrust) * Time.deltaTime * forwardSwimSpeed, ForceMode.Acceleration);
}
if (Input.GetKey(KeyCode.A))
{
//rb.AddRelativeForce(new Vector3(-1, 0, 0) * thrust);
rb.AddForce(orientation.transform.TransformDirection(new Vector3(-1, 0, 0) * thrust) * Time.deltaTime * forwardSwimSpeed, ForceMode.Acceleration);
}
if (Input.GetKey(KeyCode.D))
{
//rb.AddRelativeForce(new Vector3(1, 0, 0) * thrust);
rb.AddForce(orientation.transform.TransformDirection(new Vector3(1, 0, 0) * thrust) * Time.deltaTime * forwardSwimSpeed, ForceMode.Acceleration);
}
if (Input.GetKey(KeyCode.Space))
{
//rb.AddRelativeForce(new Vector3(0, 1, 0) * thrust);
rb.AddForce(orientation.transform.TransformDirection(new Vector3(0, 1, 0) * thrust) * Time.deltaTime * forwardSwimSpeed, ForceMode.Acceleration);
}
if (Input.GetKey(KeyCode.LeftControl))
{
//rb.AddRelativeForce(new Vector3(0, -1, 0) * thrust);
rb.AddForce(orientation.transform.TransformDirection(new Vector3(0, -1, 0) * thrust) * Time.deltaTime * forwardSwimSpeed, ForceMode.Acceleration);
}
}
void spaceRotation()
{
if (Input.GetKey(KeyCode.Q))
{
rb.transform.Rotate(new Vector3(0, 0, 1) * thrust * Time.deltaTime);
}
if (Input.GetKey(KeyCode.E))
{
rb.transform.Rotate(new Vector3(0, 0, -1) * thrust * Time.deltaTime);
}
}
private float desiredX;
private void Look()
{
float mouseX = Input.GetAxis("Mouse X") * sensitivity * Time.fixedDeltaTime * sensMultiplier;
float mouseY = Input.GetAxis("Mouse Y") * sensitivity * Time.fixedDeltaTime * sensMultiplier;
Vector3 rot = camerObj.transform.localRotation.eulerAngles;
desiredX = rot.y + mouseX;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
camerObj.transform.localRotation = Quaternion.Euler(xRotation, desiredX, 0);
orientation.transform.localRotation = Quaternion.Euler(0, desiredX, 0);
}
摆脱方向对象并改用相机似乎可以解决问题。
我的层次结构如下:
这是您的代码的更新版本:
using UnityEngine;
public class TestScript : MonoBehaviour
{
public Rigidbody targetRigidbody;
public Camera targetCamera;
public float camRotationSpeed = 5f;
public float cameraMinimumY = -60f;
public float cameraMaximumY = 75f;
public float rotationSmoothSpeed = 10f;
public float maxSpeed = 9f;
public float thrust = 20f;
public float forwardSwimSpeed = 45f;
private float xRotation;
private const float Sensitivity = 50f;
private const float SensitivityMultiplier = 1f;
private float desiredX;
private void Update()
{
SpaceRotation();
Look();
}
private void FixedUpdate()
{
if (targetRigidbody.velocity.magnitude > maxSpeed)
{
targetRigidbody.velocity = targetRigidbody.velocity.normalized * maxSpeed;
}
if (Input.GetKey(KeyCode.W))
AddForce(Vector3.forward);
if (Input.GetKey(KeyCode.S))
AddForce(Vector3.back);
if (Input.GetKey(KeyCode.A))
AddForce(Vector3.left);
if (Input.GetKey(KeyCode.D))
AddForce(Vector3.right);
if (Input.GetKey(KeyCode.Space))
AddForce(Vector3.up);
if (Input.GetKey(KeyCode.LeftControl))
AddForce(Vector3.down);
}
private void AddForce(Vector3 direction)
{
float scaledForwardSwimSpeed = Time.deltaTime * forwardSwimSpeed;
targetRigidbody.AddForce(targetCamera.transform.TransformDirection(
direction * thrust) * scaledForwardSwimSpeed,
ForceMode.Acceleration);
}
private void SpaceRotation()
{
// To avoid multiplying a vector twice
float scaledThrust = thrust * Time.deltaTime;
if (Input.GetKey(KeyCode.Q))
targetRigidbody.transform.Rotate(Vector3.forward * scaledThrust);
if (Input.GetKey(KeyCode.E))
targetRigidbody.transform.Rotate(Vector3.back * scaledThrust);
}
private void Look()
{
float mouseX = Input.GetAxis("Mouse X") * Sensitivity * Time.fixedDeltaTime * SensitivityMultiplier;
float mouseY = Input.GetAxis("Mouse Y") * Sensitivity * Time.fixedDeltaTime * SensitivityMultiplier;
Vector3 rotation = targetCamera.transform.localRotation.eulerAngles;
desiredX = rotation.y + mouseX;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
targetCamera.transform.localRotation = Quaternion.Euler(xRotation, desiredX, 0);
}
}
我目前正在研究零重力 fps 控件并且几乎所有的东西都按我想要的方式工作,但是我有一个问题,当我旋转角色和相机时,控件不会改变恰好适合。例如。宪章是水平的,但控件仍然像垂直一样。
有办法解决这个问题吗?这是我正在使用的代码。我对编码还很陌生,所以请多多关照。
public Rigidbody rb;
public Transform camerObj;
public Transform orientation;
public float camRotationSpeed = 5f;
public float cameraMinimumY = -60f;
public float cameraMaximumY = 75f;
public float rotationSmoothSpeed = 10f;
public float maxSpeed = 9f;
public float thrust = 20f;
public float forwardSwimSpeed = 45f;
private float xRotation;
private float sensitivity = 50f;
private float sensMultiplier = 1f;
void Update()
{
spaceRotation();
Look();
}
void FixedUpdate()
{
if (rb.velocity.magnitude > maxSpeed)
{
rb.velocity = rb.velocity.normalized * maxSpeed;
}
if (Input.GetKey(KeyCode.W))
{
rb.AddForce(camerObj.transform.TransformDirection(new Vector3(0, 0, 1) * thrust) * Time.deltaTime * forwardSwimSpeed, ForceMode.Acceleration);
}
if (Input.GetKey(KeyCode.S))
{
rb.AddForce(camerObj.transform.TransformDirection(new Vector3(0, 0, -1) * thrust) * Time.deltaTime * forwardSwimSpeed, ForceMode.Acceleration);
}
if (Input.GetKey(KeyCode.A))
{
//rb.AddRelativeForce(new Vector3(-1, 0, 0) * thrust);
rb.AddForce(orientation.transform.TransformDirection(new Vector3(-1, 0, 0) * thrust) * Time.deltaTime * forwardSwimSpeed, ForceMode.Acceleration);
}
if (Input.GetKey(KeyCode.D))
{
//rb.AddRelativeForce(new Vector3(1, 0, 0) * thrust);
rb.AddForce(orientation.transform.TransformDirection(new Vector3(1, 0, 0) * thrust) * Time.deltaTime * forwardSwimSpeed, ForceMode.Acceleration);
}
if (Input.GetKey(KeyCode.Space))
{
//rb.AddRelativeForce(new Vector3(0, 1, 0) * thrust);
rb.AddForce(orientation.transform.TransformDirection(new Vector3(0, 1, 0) * thrust) * Time.deltaTime * forwardSwimSpeed, ForceMode.Acceleration);
}
if (Input.GetKey(KeyCode.LeftControl))
{
//rb.AddRelativeForce(new Vector3(0, -1, 0) * thrust);
rb.AddForce(orientation.transform.TransformDirection(new Vector3(0, -1, 0) * thrust) * Time.deltaTime * forwardSwimSpeed, ForceMode.Acceleration);
}
}
void spaceRotation()
{
if (Input.GetKey(KeyCode.Q))
{
rb.transform.Rotate(new Vector3(0, 0, 1) * thrust * Time.deltaTime);
}
if (Input.GetKey(KeyCode.E))
{
rb.transform.Rotate(new Vector3(0, 0, -1) * thrust * Time.deltaTime);
}
}
private float desiredX;
private void Look()
{
float mouseX = Input.GetAxis("Mouse X") * sensitivity * Time.fixedDeltaTime * sensMultiplier;
float mouseY = Input.GetAxis("Mouse Y") * sensitivity * Time.fixedDeltaTime * sensMultiplier;
Vector3 rot = camerObj.transform.localRotation.eulerAngles;
desiredX = rot.y + mouseX;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
camerObj.transform.localRotation = Quaternion.Euler(xRotation, desiredX, 0);
orientation.transform.localRotation = Quaternion.Euler(0, desiredX, 0);
}
摆脱方向对象并改用相机似乎可以解决问题。
我的层次结构如下:
这是您的代码的更新版本:
using UnityEngine;
public class TestScript : MonoBehaviour
{
public Rigidbody targetRigidbody;
public Camera targetCamera;
public float camRotationSpeed = 5f;
public float cameraMinimumY = -60f;
public float cameraMaximumY = 75f;
public float rotationSmoothSpeed = 10f;
public float maxSpeed = 9f;
public float thrust = 20f;
public float forwardSwimSpeed = 45f;
private float xRotation;
private const float Sensitivity = 50f;
private const float SensitivityMultiplier = 1f;
private float desiredX;
private void Update()
{
SpaceRotation();
Look();
}
private void FixedUpdate()
{
if (targetRigidbody.velocity.magnitude > maxSpeed)
{
targetRigidbody.velocity = targetRigidbody.velocity.normalized * maxSpeed;
}
if (Input.GetKey(KeyCode.W))
AddForce(Vector3.forward);
if (Input.GetKey(KeyCode.S))
AddForce(Vector3.back);
if (Input.GetKey(KeyCode.A))
AddForce(Vector3.left);
if (Input.GetKey(KeyCode.D))
AddForce(Vector3.right);
if (Input.GetKey(KeyCode.Space))
AddForce(Vector3.up);
if (Input.GetKey(KeyCode.LeftControl))
AddForce(Vector3.down);
}
private void AddForce(Vector3 direction)
{
float scaledForwardSwimSpeed = Time.deltaTime * forwardSwimSpeed;
targetRigidbody.AddForce(targetCamera.transform.TransformDirection(
direction * thrust) * scaledForwardSwimSpeed,
ForceMode.Acceleration);
}
private void SpaceRotation()
{
// To avoid multiplying a vector twice
float scaledThrust = thrust * Time.deltaTime;
if (Input.GetKey(KeyCode.Q))
targetRigidbody.transform.Rotate(Vector3.forward * scaledThrust);
if (Input.GetKey(KeyCode.E))
targetRigidbody.transform.Rotate(Vector3.back * scaledThrust);
}
private void Look()
{
float mouseX = Input.GetAxis("Mouse X") * Sensitivity * Time.fixedDeltaTime * SensitivityMultiplier;
float mouseY = Input.GetAxis("Mouse Y") * Sensitivity * Time.fixedDeltaTime * SensitivityMultiplier;
Vector3 rotation = targetCamera.transform.localRotation.eulerAngles;
desiredX = rotation.y + mouseX;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
targetCamera.transform.localRotation = Quaternion.Euler(xRotation, desiredX, 0);
}
}