我有一個非常基本的Rigidbody玩家控制器和一個移動平台腳本。当玩家站在移動平台上时,玩家會與平台一起移動。

我認為主要問題是與我的球形碰撞器相關的物理材料和摩擦力。 我在程式碼中模擬摩擦力。 为了避免與摩擦力作對抗,我添加了0摩擦力材料。然而,不論摩擦力有多高,如果平台的速度很快,玩家仍然會滑行。

https://reddit.com/link/1tn5u0j/video/cfab6hxom93h1/player

當平台加速時,如何停止滑行?

private void FixedUpdate() {
  _isGrounded = CheckGrounded();

  Gravity();
  Platform();
  Movement();

  _rigidbody.linearVelocity = _velocity + _platform;
}

private void Movement() {
  var acceleration = 15f;
  const float baseSpeed = 10f;

  Vector3 facing = transform.TransformDirection(_direction);
  float verticalVelocity = _velocity.y;

  if (_isGrounded) {
    _velocity = Vector3.Lerp(_velocity, facing * baseSpeed, acceleration * Time.deltaTime);
  } else {
    Vector3 horizontalVelocity = Vector3.ProjectOnPlane(_velocity, Vector3.up);

    // 保留空中動能
    float speedCap = Mathf.Max(horizontalVelocity.magnitude, baseSpeed);

    horizontalVelocity += facing * acceleration * Time.deltaTime;

    _velocity = Vector3.ClampMagnitude(horizontalVelocity, speedCap);
  }

  Debug.Log(Vector3.ProjectOnPlane(_velocity, Vector3.up));

  _velocity.y = verticalVelocity;
}

private void Platform() {
  if (_isGrounded && _ground.rigidbody) {
    _platform = _ground.rigidbody.GetPointVelocity(_ground.point);
  } else {
    _velocity += _platform;
    _platform = Vector3.zero;
  }
}

注意:你可能會問“為什麼不用CharacterController?”。 CharacterController有嚴重的缺陷,防止玩家沿著X和Z軸旋轉。 我正在嘗試添加重力井。