y 轴限制在特定角度的 FPS 相机 [英] FPS camera with y-axis limit to certain angle
本文介绍了y 轴限制在特定角度的 FPS 相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开始一个新游戏,现在我的玩家可以查看 360*,但我想限制玩家在天空中向上和向下(直接向上和向下)查看的距离.这是我的代码
I am starting a new game and right now my player can view 360* but I want to limit how far the player looks up in the sky and down (straight up and down). Here's my code
Vector2 mouseLook;
Vector2 smoothV;
public float sensitivity = 5.0f;
public float smoothing = 2.0f;
GameObject player;
void Start () {
player = this.transform.parent.gameObject;
}
// Update is called once per frame
void Update () {
var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
mouseLook += smoothV;
transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
player.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, player.transform.up);
}
}
推荐答案
你必须使用 Mathf.Clamp
来做到这一点.下面是我用来向上/向下和向左/向右旋转相机的内容.您可以将 yMaxLimit
和 yMinLimit
变量修改为您想要限制的角度.在x
方向移动时应该没有限制.
You have to use Mathf.Clamp
to do this. Below is what I use to rotate the camera up/down and left/right. You can modify the yMaxLimit
and yMinLimit
variables to the angles you want to limit it to. There should be no limit while moving in the x
direction.
public float xMoveThreshold = 1000.0f;
public float yMoveThreshold = 1000.0f;
public float yMaxLimit = 45.0f;
public float yMinLimit = -45.0f;
float yRotCounter = 0.0f;
float xRotCounter = 0.0f;
Transform player;
void Start()
{
player = Camera.main.transform;
}
// Update is called once per frame
void Update()
{
xRotCounter += Input.GetAxis("Mouse X") * xMoveThreshold * Time.deltaTime;
yRotCounter += Input.GetAxis("Mouse Y") * yMoveThreshold * Time.deltaTime;
yRotCounter = Mathf.Clamp(yRotCounter, yMinLimit, yMaxLimit);
//xRotCounter = xRotCounter % 360;//Optional
player.localEulerAngles = new Vector3(-yRotCounter, xRotCounter, 0);
}
这篇关于y 轴限制在特定角度的 FPS 相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文