【发布时间】: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 位置。这是一个图表:
.
【问题讨论】:
-
可以在堆栈溢出时询问非常特定于游戏开发的问题,但实际上,您可能更希望在Game Development 上获得帮助。参见例如Unity: Water shader vertex offset - how to add mesh collider which updates with vertex offset? 或 How do you animate/collide against a tessellated mesh? 或 Advance efficient water physics simulation。
标签: c# unity3d geometry game-physics vertices