【问题标题】:How to make objects float on water in UnityUnity中如何让物体漂浮在水面上
【发布时间】:2021-11-13 23:22:59
【问题描述】:

我一直在为团队 seas game jam 开发一款游戏,并且我正在尝试制作一艘可以真正漂浮的船,但我遇到了一个问题,所以用当前脚本的方式来解释它有效的是它检查船上某个点的位置,如果这些点低于飞机/水的 y 水平线,那么它将向该点增加一个向上的力。这是代码:

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

public class Floater : MonoBehaviour
{
    public float depthBeforeSubmerged = 1f;
    public float displacementAmount = 3f;
    public Transform[] floatPoints;
    public Transform Water;

    private Rigidbody rb;

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

    void FixedUpdate()
    {
        foreach (Transform t in floatPoints)
        {
            if (t.position.y < Water.position.y) {
                // if point is below water add force to said p
    
                float displacementMultiplyer = Mathf.Clamp01(-t.position.y / depthBeforeSubmerged) 
                    * displacementAmount;

                rb.AddForceAtPosition(new Vector3(0f, Mathf.Abs(Physics.gravity.y) * 
                displacementMultiplyer, 0f), t.position,ForceMode.Acceleration);
            }
        }
    }
}

此代码工作正常,但问题是当我想向飞机/水中添加波浪时,船开始漂浮在水下,因为着色器将编辑网格平面而不是 y 姿势 它不会检测到飞机/水,并认为水只是静止不动,没有高度变化。所以我想要做的是让顶点在与点相同的 x 和 z 位置,然后到达 y 位置。这是一个图表:

.

【问题讨论】:

标签: c# unity3d geometry game-physics vertices


【解决方案1】:

我会从立方体的中心直接向上做一个 racyast,并根据光线投射的距离向上添加一个力,所以当它在水下越低时,它会被强制向上。我不知道在基础物理引擎中是否有任何具体的方法。

【讨论】:

  • 这可能会奏效,但直到稍后,我将无法尝试,因为游戏 Jam 将于 29 日结束,如果它不起作用,我可能最终会失去游戏其他方面的一些工作.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-20
  • 2018-09-21
  • 2014-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-02
相关资源
最近更新 更多