Unity3d 统一fps旋转摄像机

Unity3d 统一fps旋转摄像机,unity3d,Unity3d,在我的游戏中,我有一个相机,我想有一个FPS一样的旋转连接到这个相机 因此,如果我将光标向左移动,我希望凸轮向左旋转。如果我向上移动光标,那么凸轮应该向上看,等等 我目前有它的部分工作。我可以左右、上下看。当我向下看然后左右移动光标时,就会出现问题。然后它给我一个“滚动”效果 请观看此视频以了解我的确切意思: 显然,当我向下看时,我仍然希望有“偏航”效果,而不是“滚动”效果。有人知道怎么做吗?这就是我到目前为止所做的: //每帧调用一次更新 公共覆盖无效更新() { this.camera.t

在我的游戏中,我有一个相机,我想有一个FPS一样的旋转连接到这个相机

因此,如果我将光标向左移动,我希望凸轮向左旋转。如果我向上移动光标,那么凸轮应该向上看,等等

我目前有它的部分工作。我可以左右、上下看。当我向下看然后左右移动光标时,就会出现问题。然后它给我一个“滚动”效果

请观看此视频以了解我的确切意思:

显然,当我向下看时,我仍然希望有“偏航”效果,而不是“滚动”效果。有人知道怎么做吗?这就是我到目前为止所做的:

//每帧调用一次更新
公共覆盖无效更新()
{
this.camera.transform.rotation*=
四元数角度轴(Time.deltaTime*sensitivityRoll*Input.GetAxis(“垂直”),矢量3.forward);
this.camera.transform.rotation*=
四元数角度轴(Time.deltaTime*sensitivityYaw*Input.GetAxis(“鼠标X”),矢量3.up);
this.camera.transform.rotation*=
四元数角度轴(Time.deltaTime*sensitivitypatch*Input.GetAxis(“鼠标Y”),向量3.left);
}

我刚刚在这个主题中找到了答案:


该主题中的代码:

C#单声道代码:

使用UnityEngine;
使用系统集合;
///MouseLook基于鼠标增量旋转变换。
///最小值和最大值可用于约束可能的旋转
///要生成FPS样式的字符,请执行以下操作:
///-创建一个胶囊。
///-将MouseLook脚本添加到胶囊中。
///->将鼠标外观设置为使用LookX。(您只想旋转角色,但不想倾斜角色)
///-将FPInputController脚本添加到胶囊中
///->将自动添加CharacterMotor和CharacterController组件。
///-创建一个摄像头。让相机成为胶囊的孩子。重置它的转换。
///-向相机添加鼠标悬停脚本。
///->将鼠标外观设置为使用LookY。(您希望摄影机像头部一样上下倾斜。角色已旋转。)
[添加组件菜单(“摄像头控制/鼠标外观”)]
公共级鼠标库:单行为{
公共枚举循环数{MouseXAndY=0,MouseX=1,MouseY=2}
公共旋转轴=旋转轴.MouseXAndY;
公共浮子灵敏度x=15F;
公众浮标灵敏度yy=15F;
公共浮动最小值x=-360F;
公共浮动最大值x=360F;
公共浮动最小值=-60F;
公共浮动最大值=60F;
浮动旋转Y=0F;
无效更新()
{
如果(轴==旋转轴.MouseXAndY)
{
float rotationX=transform.localEulerAngles.y+Input.GetAxis(“鼠标X”)*sensitivityX;
旋转Y+=输入.GetAxis(“鼠标Y”)*灵敏度yy;
旋转Y=数学夹具(旋转Y、最小值、最大值);
transform.localEulerAngles=新矢量3(-rotationY,rotationX,0);
}
else if(axes==RotationAxes.MouseX)
{
transform.Rotate(0,Input.GetAxis(“鼠标X”)*sensitivityX,0);
}
其他的
{
旋转Y+=输入.GetAxis(“鼠标Y”)*灵敏度yy;
旋转Y=数学夹具(旋转Y、最小值、最大值);
transform.localEulerAngles=新矢量3(-rotationY,transform.localEulerAngles.y,0);
}
}
无效开始()
{
//如果(!networkView.isMine)
//启用=错误;
//使刚体不改变旋转
//if(刚体)
//刚体旋转=真;
}
}

我为我的FPS项目创建了两个脚本,它们对我的动作完美无瑕(目前还没有添加跳跃机制),看起来你已经将许多简单直接的事情过度复杂化了。 我也是新的编码,所以请纠正我,如果我错了,这是我的代码

//这是在播放器脚本中,我把它放在相机和播放器精灵的空父对象上

public class player : MonoBehaviour
{
    [Header("player Movement")]
    [SerializeField] float moveSpeed = 5f;
    [SerializeField] public float mouseSensitivity = 5f;

    
    Rigidbody rigidBody;
   
    // Use this for initialization
    void Start()
    {
        
    }

 void Update()
    {
        Move();
       
    }


    private void Move()
    {
        transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime);
        transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime);
        transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivity);
    }
//这是我放在相机上的相机移动脚本(也将使用此脚本添加一个功能,以将某些能力从第一人称更改为第三人称)

{
玩家;
//用于初始化
void Start()
{
player=FindObjectOfType();
}
//每帧调用一次更新
无效更新()
{
MoveView();
}
私有void MoveView()
{
transform.Rotate(Vector3.left*Input.GetAxis(“鼠标Y”)*player.mouseSensitivity);
}
}

简单的脚本,不确定它是否适合您,不妨尝试一下

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;

public class mouse_lookat : MonoBehaviour
{
    public float mouseSensitivity = 100f;

    public Transform playerBody;

    float xRotation = 0f;

    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        playerBody.Rotate(Vector3.up * mouseX);
    }
}

几年过去了,但是谢谢你的回答,这正是我想要的!为什么
使用System.Security.Cryptography??
GetAxis
返回自上一帧以来鼠标绝对移动的差值。不要——我重复一遍,不要——将鼠标轴的
GetAxis
乘以
deltaTime
,除非您希望鼠标移动的速度比鼠标移动的距离更重要。这在大多数情况下都是不直观的,尤其是这一次!而且,即使你这样做了,在这些情况下,你的意思可能是除以deltaTime(因为速度=delta位置/delta时间)
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;

public class mouse_lookat : MonoBehaviour
{
    public float mouseSensitivity = 100f;

    public Transform playerBody;

    float xRotation = 0f;

    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        playerBody.Rotate(Vector3.up * mouseX);
    }
}