无法使我的相机正确限制其旋转 [英] Can't Get My Camera To Limit Its Rotation Properly
问题描述
在我的游戏中,我想夹住玩家的相机,使其不能进行前空翻或后空翻.我想将x轴限制在-75到50之间,但是那样行不通.
In my game, I want to clamp the player's camera so it can't do frontflips or backflips. I want to clamp the x-axis between -75 and 50 but it just won't work.
每次我在代码中添加一个夹具时,相机都不想将其旋转方向从输入的0,0,0移到比Input.GetAxisRaw说的鼠标移动的位置.
Every time I add a clamp such as the one in my code, the camera doesn't want to move it's rotation from 0,0,0 farther than Input.GetAxisRaw says the mouse is moving.
我也尝试过使用if语句作为手动钳位,但是如果我切换极性,它要么一直保持在0,0,0,要么一直保持在-75,0,0.
I've tried using if statements as manual clamps too but it either stays at 0,0,0 constantly or -75,0,0 constantly if I switch the polarity.
我曾尝试更换相机,以防它与设置有关,但没有任何变化.
I've tried replacing the camera in case it was something related to its settings but nothing changes.
我不想发布它,因为它不应该那么难,但是我已经花了很多天了,而且我选择不了.任何想法都值得赞赏.
I didn't want to post this because it shouldn't be this hard but I've spent multiple days on this and I'm out of options; any and all ideas are much appreciated.
我正在使用Visual Studio作为编辑器.
I am using Visual Studio as my editor.
using UnityEngine;
public class PlayerMove : MonoBehaviour {
Rigidbody rb;
public Camera cam;
public Transform camTrans;
Vector3 movement;
Vector3 rotation;
public float sensitivityX;
public float sensitivityY;
public float playerSpeed;
public float jumpForce;
float forward;
float sideways;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
forward = Input.GetAxisRaw("Vertical");
sideways = Input.GetAxisRaw("Horizontal");
movement = new Vector3 (forward, 0, -sideways).normalized;
float _xRot = Input.GetAxisRaw("Mouse Y");
float _yRot = Input.GetAxisRaw("Mouse X");
rotation = new Vector3(0f, _yRot, 0f) * sensitivityX;
float _jump = Input.GetAxisRaw("Jump");
if (movement != Vector3.zero)
{
MovePlayer();
}
if (rotation != Vector3.zero && Input.GetAxisRaw("Fire2") != 0 || _xRot != 0 && Input.GetAxisRaw("Fire2") != 0)
{
Rotate(-_xRot);
}
if (_jump != 0f)
{
Jump();
}
}
void MovePlayer()
{
float _playerSpeed;
_playerSpeed = playerSpeed * 0.1f;
transform.Translate(movement * _playerSpeed * Time.fixedDeltaTime, Space.Self);
}
void Jump()
{
if (IsGrounded())
{
rb.AddForce(new Vector3(0, 1 * jumpForce, 0), ForceMode.Impulse);
}
}
void Rotate(float _camRot)
{
camTrans.Rotate(new Vector3(_camRot * sensitivityY, 0, 0));
float _camPosX = camTrans.rotation.x;
Mathf.Clamp(_camPosX, -75, 50);
camTrans.rotation = Quaternion.Euler(new Vector3(_camPosX, 0, 0));
rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation * sensitivityX));
}
bool IsGrounded()
{
RaycastHit hit;
return Physics.Raycast(transform.position, Vector3.down, out hit, 1.001f);
}
}
推荐答案
Input.GetAxisRaw("Mouse Y");
返回鼠标移动的单位数(请参见
Input.GetAxisRaw("Mouse Y");
returns the number of units the mouse moved (See Unity Documentation - Input.GetAxisRaw).
当您移动鼠标时,脚本可以工作,但是由于void Rotate()
在Update()
函数中调用了每个帧,因此在某些帧Input.GetAxisRaw("Mouse Y")
之后;返回0,然后返回_camRot = 0
.
When you move your mouse, the script works, but because void Rotate()
called each frame in Update()
function, after some frames Input.GetAxisRaw("Mouse Y")
; returns 0 and then _camRot = 0
.
所以camTrans.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
,并且由于Update()
每秒被调用多次,我们认为相机始终保持在0,0,0.
So, camTrans.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
and because Update()
called multiple times per second, We think camera stays at 0,0,0 constantly.
要限制旋转,您可以将代码更改为此:
To clamp the rotation, you can change your code to this:
public class PlayerMove : MonoBehaviour
{
...
private float rotationX;
...
void Rotate(float _camRot)
{
rotationX += _camRot * sensitivityY;
rotationX = Mathf.Clamp(rotationX, -75, 50);
camTrans.localEulerAngles = new Vector3(rotationX, camTrans.localEulerAngles.y, camTrans.localEulerAngles.z);
rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation * sensitivityX));
}
}
希望对您有帮助.
这篇关于无法使我的相机正确限制其旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!