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

如何设置特定加速度?平台游戏角色运动参数配置求助

平台游戏盒子角色移动实现方案

核心思路

放弃直接依赖AddForce硬加力的方式,改用目标速度驱动+平滑阻尼的逻辑,能精准控制加速/减速时长,同时方便结合质量调整加速效率,完全匹配你的需求。

关键参数配置

先在脚本中定义可调试的参数(挂在角色物体上,Unity编辑器里直接调整):

  • maxMoveSpeed:角色最大移动速度(示例值:8)
  • accelerationTime:从静止到最大速度的耗时(示例值:2秒)
  • decelerationTime:从最大速度到静止的耗时(示例值:1秒)

代码实现(C#)

using UnityEngine;

public class BoxMovement : MonoBehaviour
{
    public float maxMoveSpeed = 8f;
    public float accelerationTime = 2f;
    public float decelerationTime = 1f;

    private Rigidbody2D _rb;
    private float _targetSpeed;
    private float _baseAccelerationRate;
    private float _decelerationRate;

    void Start()
    {
        _rb = GetComponent<Rigidbody2D>();
        // 计算质量为1时的基础加速速率
        _baseAccelerationRate = maxMoveSpeed / accelerationTime;
        // 计算减速速率
        _decelerationRate = maxMoveSpeed / decelerationTime;
    }

    void Update()
    {
        // 获取水平输入(A/D或方向键)
        float input = Input.GetAxisRaw("Horizontal");
        // 设定目标速度
        _targetSpeed = input != 0 ? input * maxMoveSpeed : 0;
    }

    void FixedUpdate()
    {
        float currentSpeed = _rb.velocity.x;
        float speedDiff = _targetSpeed - currentSpeed;

        float appliedRate;
        // 判断当前是加速还是减速状态
        if (Mathf.Sign(speedDiff) == Mathf.Sign(currentSpeed) || currentSpeed == 0)
        {
            // 加速阶段:质量越大,加速速率越低,耗时越长
            appliedRate = _baseAccelerationRate / _rb.mass;
        }
        else
        {
            // 减速阶段:使用更快的固定速率
            appliedRate = _decelerationRate;
        }

        // 计算所需的力(F = m*a)
        float force = _rb.mass * appliedRate * Mathf.Sign(speedDiff);
        // 限制力的大小,避免速度突变
        force = Mathf.Clamp(force, -Mathf.Abs(_rb.mass * appliedRate), Mathf.Abs(_rb.mass * appliedRate));
        
        _rb.AddForce(new Vector2(force, 0), ForceMode2D.Force);

        // 强制限制最大速度,防止过冲
        if (Mathf.Abs(_rb.velocity.x) > maxMoveSpeed)
        {
            _rb.velocity = new Vector2(Mathf.Sign(_rb.velocity.x) * maxMoveSpeed, _rb.velocity.y);
        }
    }
}

代码说明

  1. 质量影响加速:加速阶段的速率除以了物体质量,质量为3时,加速速率是质量1时的1/3,达到最大速度的时间正好是原来的3倍,完全符合需求。
  2. 减速更快:减速阶段直接使用固定的高速率,不受质量影响,确保减速耗时远短于加速。
  3. 速度钳制:最后添加的速度限制逻辑,避免因力的叠加导致速度超过设定的最大值。
  4. Raw输入:用Input.GetAxisRaw获取输入,跳过Unity自带的输入平滑,让我们完全掌控加速减速的曲线。

纯AddForce替代方案(精准度稍低)

如果一定要用AddForce直接控制,可通过固定力的方式实现质量影响加速度的效果,但加速时长需要手动调试:

using UnityEngine;

public class BoxForceMovement : MonoBehaviour
{
    public float maxMoveSpeed = 8f;
    public float accelerationForce = 10f;
    public float decelerationForce = 20f;

    private Rigidbody2D _rb;

    void Start()
    {
        _rb = GetComponent<Rigidbody2D>();
    }

    void FixedUpdate()
    {
        float input = Input.GetAxisRaw("Horizontal");
        if (input != 0)
        {
            // 固定加速力,质量越大加速度越小(a=F/m)
            _rb.AddForce(new Vector2(input * accelerationForce, 0));
        }
        else
        {
            // 施加反向力快速减速
            if (Mathf.Abs(_rb.velocity.x) > 0.1f)
            {
                float decel = -Mathf.Sign(_rb.velocity.x) * decelerationForce;
                _rb.AddForce(new Vector2(decel, 0));
            }
            else
            {
                _rb.velocity = new Vector2(0, _rb.velocity.y);
            }
        }

        // 限制最大速度
        if (Mathf.Abs(_rb.velocity.x) > maxMoveSpeed)
        {
            _rb.velocity = new Vector2(Mathf.Sign(_rb.velocity.x) * maxMoveSpeed, _rb.velocity.y);
        }
    }
}

内容的提问来源于stack exchange,提问作者Seyit Şahin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 23:44:59