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

Unity使用Cinemachine时玩家3D对象移动抖动问题求助

玩家3D对象移动抖动问题(Unity开发)

我正在用Unity开发一款3D游戏,已经实现了类似《Risk of Rain 2》的第三人称自由视角相机,从后方跟随玩家。但现在遇到玩家3D渲染对象移动时抖动的问题——排查后确认抖动的是玩家对象,相机始终稳定对准玩家应处的位置,不会造成环境抖动。推测是Unity设置或代码逻辑导致的类似“相机挂载到Rigidbody引发抖动”的问题。

代码实现

using System;
using System.Collections;
using System.Collections.Generic;
using Cinemachine;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;

public class PlayerMovement : MonoBehaviour
{
    public GameObject thirdPersonCamera;
    public float speed;
    public float jumpForce;
    public LayerMask whatIsGround;
    public float jumpCooldown;
    
    private Vector3 moveDirection;
    private float horizontalInput;
    private float verticalInput;
    private Rigidbody playerRb;
    private bool grounded;
    private bool readyToJump;
    
    // Start is called before the first frame update
    void Start()
    {
        playerRb = transform.GetChild(0).GetComponent<Rigidbody>();
    }

    private void Update()
    {
        //Horizontal and vertical mouse input with Cinemachine
        horizontalInput = thirdPersonCamera.GetComponent<CinemachineFreeLook>().m_XAxis.Value;
        verticalInput = Input.GetAxisRaw("Vertical");
        
        //Check if the player is grounded
        grounded = Physics.Raycast(transform.position, Vector3.down, 1 * 0.5f + 0.3f, whatIsGround);
        
        PlayerInputToMove();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        transform.rotation = Quaternion.Euler(transform.rotation.x, horizontalInput, transform.rotation.z).normalized;
    }

    //Recieves input and moves the player
    private void PlayerInputToMove()
    {
        if (Input.GetKey(KeyCode.W))
        {
            transform.Translate(Vector3.forward * speed * Time.deltaTime, Space.Self);
        }
        if (Input.GetKey(KeyCode.A))
        {
            transform.Translate(Vector3.left * speed * Time.deltaTime, Space.Self);
        }
        if (Input.GetKey(KeyCode.S))
        {
            transform.Translate(Vector3.back * speed * Time.deltaTime, Space.Self);
        }
        if (Input.GetKey(KeyCode.D))
        {
            transform.Translate(Vector3.right * speed * Time.deltaTime, Space.Self);
        }

        if(Input.GetKey(KeyCode.Space) && readyToJump && grounded)
        {
            readyToJump = false;

            Jump();

            Invoke(nameof(ResetJump), jumpCooldown);
        }
    }
    
    private void Jump()
    {
        playerRb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
        Debug.Log("Called Jump()");
    }
    private void ResetJump()
    {
        readyToJump = true;
    }
}

场景设置

  • PlayerMovement脚本挂载在Player对象上
  • 3D视觉对象、Rigidbody及Capsule Collider均在PlayerObject对象下

已尝试的解决方案

  • 将PlayerObject的子对象CameraPoint设为相机的Follow与Look At目标,而非Player或PlayerObject
  • 将PlayerMovement脚本从Player移至PlayerObject上挂载

内容的提问来源于stack exchange,提问作者A guy with questions

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 18:46:23