【问题标题】:Unity: Faux Gravity - inner Capsule/ CylinderUnity: Faux Gravity - 内部胶囊/圆柱体
【发布时间】:2017-09-22 15:18:33
【问题描述】:

我正在与圆筒或胶囊中的虚假重力作斗争。基本上我认为我可以采用与球形重力相同的代码,但这不起作用。所以我改变了一些行以获得更好的结果。

    [SerializeField] float gravity = 10;

public void Attract ( Transform target )
{
    Vector3 gravityUp = (target.position - new Vector3(transform.position.x, transform.position.y,target.position.z)).normalized;
    Vector3 bodyDown = -target.up;

    Rigidbody rb = target.GetComponent<Rigidbody>();
    rb.AddForce(gravityUp * gravity);

    Quaternion targetRotation = Quaternion.FromToRotation(bodyDown, gravityUp) * target.rotation;
    targetRotation.x = 0;

    target.rotation = Quaternion.Slerp(target.rotation, targetRotation, 30.0f * Time.deltaTime);

}

这在第一次尝试时运行良好。但是玩家(目标)不能旋转一个 Y 轴。有人有什么想法吗?

【问题讨论】:

    标签: unity3d gravity cylindrical


    【解决方案1】:

    好的,我尝试了以下方法。

     Quaternion targetRotation = Quaternion.FromToRotation(bodyDown, gravityUp) * target.rotation;
    targetRotation.x = 0;
    
    target.rotation = Quaternion.Slerp(target.rotation, targetRotation, 30.0f * Time.deltaTime);
    

    现在我使用 Surface 法线来旋转播放器。

    if (Physics.Raycast(attractedBody.transform.position + attractedBody.transform.forward, -attractedBody.transform.up, out hit, distance))
        {
            surfaceNorm = hit.normal;
        }
    

    但是如果没有 Rigidbody Contrains,播放器会在没有任何输入的情况下开始旋转。所以我必须使用:

    rb.constraints = RigidbodyConstraints.FreezeRotation;
    

    这行得通。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-03
      • 1970-01-01
      • 2014-01-03
      • 2012-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-08
      相关资源
      最近更新 更多