繁体   English   中英

第一人称相机旋转倾斜 Unity3d c#

[英]First person camera rotation tilted Unity3d c#

**``` 我正在制作一个游戏,您可以在其中控制机关枪并试图杀死敌人。 没有真正的播放器,只有一个摄像头。 我需要游戏的第一人称视角,所以我在关卡 1 中添加了一些简单的脚本。如果你输了,关卡会重置。 但是,无论何时运行它,相机旋转都会被触发,即使它设置为 0,0,0。 在你死后重生后,由于某种原因,相机正常,场景被重置。 我还注意到相机有时会倾斜



using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CrosshairMove : MonoBehaviour
{
    public float sens = 100f;
    public Rigidbody rb;
    public Transform cam;
    float xRotation = 0f;
    float yRotation = 0f;
    public float sceneused = 0f;
    public float currentscene = 0f;
    


    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }


    
    void FixedUpdate()
    {
        float mouseX = Input.GetAxis("Mouse X") * sens * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * sens * Time.deltaTime;

        xRotation -= mouseY;
        yRotation -= mouseX;
        //xRotation -= Mathf.Clamp(xRotation, -90f, 90f);
        //transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        cam.Rotate(Vector3.up * mouseX);
        cam.Rotate(Vector3.left * mouseY);
    }
}
    




    

我会:

  1. 尝试不同的游戏对象进行独立旋转,避免云台锁定 为此,您可以将相机放在子 object 中,当父在其局部轴上旋转时,子“遭受”相同的旋转。
  2. Clamp xrot 180º 在 FPS 相机中很常见
  3. 仅对物理使用FixedUpdate 对于其他任何事情,请使用正常Update

以上所有内容的建议片段:

public GameObject parentGO; // drag ref to parent go here in the editor    

void Update {
    float mouseX = Input.GetAxis("Mouse X") * lookAtSensitivity * Time.deltaTime;
    float mouseY = Input.GetAxis("Mouse Y") * lookAtSensitivity * 
    Time.deltaTime;
    xRot -= mouseY;
    xRot = Mathf.Clamp(xRot, -90f, 90f);
    transform.localRotation = Quaternion.Euler(xRot, 0f, 0f);
    parentGO.transform.Rotate(Vector3.up * mouseX);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM