You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity3D中FPS脚本鼠标与输入抖动问题求助

问题描述
  • 鼠标移动控制视角时出现抖动,WASD移动功能正常
  • 跳跃功能前0.5秒浮空正常,之后角色剧烈抖动甚至掉落,代码仍在执行
  • 已尝试将Input包输入模式改为Process Events In Dynamic Update,无效

相关代码

movement.cs

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

public class movement : MonoBehaviour
{
    public float speed = 10.0f;
    private float horizontalInput;
    private float forwardInput;
    private float jumpInput;
    void Start()
    {
        
    }

    void Update()
    {
        horizontalInput = Input.GetAxis("Horizontal");
        forwardInput = Input.GetAxis("Vertical");
        jumpInput = Input.GetAxis("Jump");
        transform.Translate(Vector3.forward * Time.deltaTime * speed * forwardInput);
        transform.Translate(Vector3.right * Time.deltaTime * speed * horizontalInput);
        transform.Translate(Vector3.up * Time.deltaTime * speed * jumpInput);
    }
}

look.cs

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

public class look : MonoBehaviour
{
    public float mouseSense = 100f;
    public Transform PlayerBody;
    float Xrotation = 0f;
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    void FixedUpdate()
    {
        float mouseX = Input.GetAxis("Mouse X") * mouseSense * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSense * Time.deltaTime;
        PlayerBody.Rotate(Vector3.up * mouseX);
        Xrotation -= mouseY;
        Xrotation = Mathf.Clamp(Xrotation, -90f, 90f);
        transform.localRotation = Quaternion.Euler(Xrotation, 0f, 0f);
        
    }
}
解决方案

1. 鼠标视角抖动修复

鼠标输入属于帧同步相关操作,放在FixedUpdate(固定时间间隔执行)会导致输入采样不连贯,产生抖动。将视角控制代码移到Update中:

修改look.cs的方法:

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

2. 跳跃抖动掉落修复

直接用transform.Translate处理跳跃会忽略物理引擎规则,加上Input.GetAxis("Jump")持续触发的特性,会和重力(若存在)冲突导致抖动。改用Rigidbody处理物理类移动:

步骤1:给角色添加Rigidbody组件,取消勾选Is Kinematic,保留Use Gravity

步骤2:修改movement.cs代码:

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

public class movement : MonoBehaviour
{
    public float speed = 10.0f;
    public float jumpForce = 5f;
    private Rigidbody rb;
    private float horizontalInput;
    private float forwardInput;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        horizontalInput = Input.GetAxis("Horizontal");
        forwardInput = Input.GetAxis("Vertical");
        
        // 仅在跳跃键按下瞬间触发一次跳跃
        if (Input.GetButtonDown("Jump"))
        {
            rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
        }
    }

    // 物理相关操作放在FixedUpdate中,保证和物理引擎同步
    void FixedUpdate()
    {
        // 基于玩家朝向生成移动方向,保持水平移动
        Vector3 moveDirection = transform.forward * forwardInput + transform.right * horizontalInput;
        moveDirection.y = 0;
        rb.velocity = new Vector3(moveDirection.x * speed, rb.velocity.y, moveDirection.z * speed);
    }
}

内容的提问来源于stack exchange,提问作者teao22

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 16:33:17