我已经在团结中创造了我自己的角色,我现在正在做相机的工作,我想夹住相机的Y旋转,而我这样做的方法是正确的。
mouseRotY = Mathf.Clamp(mouseRotY, -90.0f, 90.0f);所以刚刚发生的事情是摄像机从359转到0。在我玩游戏时,除非我把鼠标向上移动,否则什么都不会发生。它使屏幕看起来像是在闪烁。
这是我的完整代码:
using UnityEngine;
using System.Collections;
public class FirstPersonController : MonoBehaviour {
CharacterController cc;
public float baseSpeed = 3.0f;
public float mouseSensitivity = 1.0f;
float mouseRotX = 0,
mouseRotY = 0;
public bool inverted = false;
float curSpeed = 3.0f;
string h = "Horizontal";
string v = "Vertical";
void Start () {
cc = gameObject.GetComponent<CharacterController>();
}
void FixedUpdate () {
curSpeed = baseSpeed;
mouseRotX = Input.GetAxis("Mouse X") * mouseSensitivity;
mouseRotY -= Input.GetAxis("Mouse Y") * mouseSensitivity;;
mouseRotY = Mathf.Clamp(mouseRotY, -90.0f, 90.0f);
if (!inverted)
mouseRotY *= -1;
else
mouseRotY *= 1;
float forwardMovement = Input.GetAxis(v);
float strafeMovement = Input.GetAxis(h);
Vector3 speed = new Vector3(strafeMovement * curSpeed, 0, forwardMovement * curSpeed);
speed = transform.rotation * speed;
cc.SimpleMove(speed);
transform.Rotate(0, mouseRotX, 0);
Camera.main.transform.localRotation = Quaternion.Euler(mouseRotY, 0 ,0);
}
}如果你们中的任何人能帮我,那就太好了。谢谢。
发布于 2014-07-18 20:20:39
你的倒置逻辑是有缺陷的,只要把它拿出来,它就能工作。要反转旋转,您只需要反转输入,而不是旋转本身--每一个帧(它只需要从+到-然后返回到+,等等)。下面是一个带有倒置标志的y旋转的简化版本,它可以工作:
using UnityEngine;
using System.Collections;
public class FirstPersonController : MonoBehaviour
{
public float mouseSensitivity = 1.0f;
float mouseRotY = 0.0f;
public bool inverted = false;
private float invertedCorrection = 1.0f;
void FixedUpdate ()
{
if(Input.GetAxis ("Fire1") > 0.0f)
inverted = !inverted;
if(inverted)
invertedCorrection = -1.0f;
else
invertedCorrection = 1.0f;
mouseRotY -= invertedCorrection * Input.GetAxis("Mouse Y") * mouseSensitivity;
mouseRotY = Mathf.Clamp(mouseRotY, -90.0f, 90.0f);
Camera.main.transform.localRotation = Quaternion.Euler(mouseRotY, 0.0f, 0.0f);
}
}您可能需要做的另一件事是在Start()函数中获得摄像机的原始旋转。现在,它将第一帧的旋转设置为零。我无法从剧本中看出这是否是预期的行为。
https://stackoverflow.com/questions/24831042
复制相似问题