Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';我不能让我的相机适当地限制它的旋转_C#_Unity3d - Fatal编程技术网

C# Can';我不能让我的相机适当地限制它的旋转

C# Can';我不能让我的相机适当地限制它的旋转,c#,unity3d,C#,Unity3d,在我的游戏中,我想夹紧玩家的相机,这样它就不能进行前翻或后翻。我想把x轴夹在-75和50之间,但它就是不起作用 每次我添加一个夹具(如代码中的夹具)时,相机都不希望将其旋转从输入的0,0,0处移动到比输入更远的位置。GetAxisRaw表示鼠标正在移动 我也尝试过使用if语句作为手动钳制,但是如果我切换极性,它要么持续保持在0,0,0,要么持续保持在-75,0,0 我尝试过更换相机,以防它与设置有关,但没有任何变化 我不想发表这篇文章,因为它不应该这么难,但我已经花了好几天在这上面,我没有选择;

在我的游戏中,我想夹紧玩家的相机,这样它就不能进行前翻或后翻。我想把x轴夹在-75和50之间,但它就是不起作用

每次我添加一个夹具(如代码中的夹具)时,相机都不希望将其旋转从输入的0,0,0处移动到比输入更远的位置。GetAxisRaw表示鼠标正在移动

我也尝试过使用if语句作为手动钳制,但是如果我切换极性,它要么持续保持在0,0,0,要么持续保持在-75,0,0

我尝试过更换相机,以防它与设置有关,但没有任何变化

我不想发表这篇文章,因为它不应该这么难,但我已经花了好几天在这上面,我没有选择;任何和所有的想法都非常感谢

我正在使用VisualStudio作为我的编辑器

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);
    }
}
使用UnityEngine;
公共类玩家运动:单一行为{
刚体rb;
公共摄像机;
公共交通;
矢量3运动;
矢量3旋转;
公共浮动敏感性x;
公众浮动敏感度y;
公众花车运动员速度;
公共安全部队;
向前浮动;
侧浮;
void Start()
{
rb=GetComponent();
}
无效固定更新()
{
前进=输入。GetAxisRaw(“垂直”);
sideways=Input.GetAxisRaw(“水平”);
移动=新矢量3(向前,0,-侧向)。标准化;
float xRot=Input.GetAxisRaw(“鼠标Y”);
float _yRot=Input.GetAxisRaw(“鼠标X”);
旋转=新矢量3(0f,旋转,0f)*灵敏度x;
float_jump=Input.GetAxisRaw(“跳转”);
if(运动!=Vector3.zero)
{
MovePlayer();
}
如果(旋转!=Vector3.0&&Input.GetAxisRaw(“Fire2”)!=0 | | | uxRot!=0&&Input.GetAxisRaw(“Fire2”)!=0)
{
旋转(-xRot);
}
如果(_jump!=0f)
{
跳跃();
}
}
void MovePlayer()
{
浮球(playerSpeed);;
_播放速度=播放速度*0.1f;
transform.Translate(移动*_playerSpeed*Time.fixeddedtime,Space.Self);
}
无效跳转()
{
如果(isground())
{
rb.AddForce(新矢量3(0,1*jumpForce,0),ForceMode.Pulse);
}
}
空心旋转(浮动_camRot)
{
凸轮变速器旋转(新矢量3(_camRot*灵敏度y,0,0));
float _camPosX=凸轮变速器旋转.x;
Mathf.夹具(_camPosX,-75,50);
camTrans.rotation=Quaternion.Euler(新向量3(_camPosX,0,0));
rb.MoveRotation(rb.rotation*四元数.Euler(rotation*sensitivityX));
}
bool-IsGrounded()
{
雷卡斯特击中;
返回物理.Raycast(transform.position,Vector3.down,outhit,1.001f);
}
}
Input.GetAxisRaw(“鼠标Y”)返回鼠标移动的单位数(请参阅)

移动鼠标时,脚本会工作,但这是因为
void Rotate()
在一些帧
Input.GetAxisRaw(“鼠标Y”)
之后调用了
Update()
函数中的每个帧;返回0,然后返回
\u camRot=0

所以,
camTrans.rotation=Quaternion.Euler(新向量3(0,0,0))Update()
,所以我们认为摄影机始终保持在0,0,0

要钳制旋转,可以将代码更改为:

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));
    }
}

我希望它能帮助您。

这与OP最初尝试的有什么区别?解释这一点将提高答案的质量。@code11当然,我的朋友。问题在于
camTrans.rotation=Quaternion.Euler(新向量3(_camPosX,0,0))行<代码>输入.GetAxisRaw(“鼠标Y”)返回鼠标移动的单位数。你可以看到。当您移动鼠标时,脚本会工作,但这是因为
void Rotate()
调用了
Update()
函数中的每个帧,在一些帧之后
Input.GetAxisRaw(“鼠标Y”)
返回0,然后
\u camRot=0
。所以,
camTrans.rotation=Quaternion.Euler(新向量3(0,0,0))
和camera保持在0,0,0。@code11但在我的代码中,我创建了变量
rotationX
。每次它
+=\u camRot*灵敏度yy并保存以前的值。另外,
rotationX=Mathf.Clamp(rotationX,-75,50)夹持-75和50之间的旋转X。最后,
camTrans.localEulerAngles=newvector3(rotationX,camTrans.localEulerAngles.y,camTrans.localEulerAngles.z)围绕X轴旋转CamTrans。你可以看到。