Hi there,
I’ll try and explain this as best I can, so bear with here for a second.
Quick and dirty:
Unity standard plane mesh with a wave like animation added to it.
In-depth:
Plane mesh (standard Unity), 10x10 rectangles.
Transformed to 0x0x0 by 0x0x0 with scale 128x1x128.
Plane (Mesh filter)
Mesh Collider, Is not trigger, material is custom (water), is not convex, is not smooth sphere collider, is based on Plane mesh.
Mesh Renderer, does cast shadows, does receive shadows, uses a custom material with built-in shader Reflective/Bumped Diffuse.
Water_Wave_Controller (custom C# script), Speed = 1, Amplitude = 5, Frequency = 64. More bout this below.
Cube mesh (standard Unity)
Transformed to 0x16x0 by 0x0x0 with scale 1x1x1
Cube (Mesh filter)
Box Collider, is not trigger, material is custom (water), convex not possible (obviously)
Mesh Renderer, blahdieblah (not relative for this issue)
RigidBody, mass = 15, drag = 0, ang drag = 0.05, uses gravity, is not kinematic, interpolate = none, collision = continous dynamic, constrains = none
The problem:
The plane mesh (the water surface) is changed and updated every cycle, but its collision mesh is not. I’m not sure how to get it to work, but I have tried about every checkbox and form of collision detection there is here and I cannot get the Cube to float in the water. I can get it to float on the collision mesh, a bit above/below the water (depending on if its inside a wave or not), but I’m looking for a way to update the collision mesh when I update the plane mesh.
The script (Water_Wave_Controller) in C#:
// Declare dependancies:
using UnityEngine;
using System.Collections;
// Declare class:
public class Water_Wave_Controller : MonoBehaviour {
// Declare subclasses:
public class Wave {
// Declare subclasses:
// Declare datatypes:
// Declare enumerators:
// Declare properties:
decimal Amplitude;
decimal Position;
decimal Moment;
decimal Frequency;
// Declare constructors:
public Wave(decimal Amplitude, decimal Frequency) {
this.Amplitude = Amplitude;
this.Frequency = Frequency;
}
// Declare methods:
public decimal Get_Position(decimal Moment) {
return this.Amplitude * (decimal)Mathf.Sin((float)this.Frequency * (float)Moment);
}
}
// Declare datatypes:
// Declare enumerators:
// Declare properties:
public float Speed = 1f;
public float Amplitude = 5f;
public float Frequency = 64f;
private Wave Pattern;
public decimal MinOffset;
public decimal MaxOffset;
private decimal CurOffset = (decimal) 0;
// Declare constructors:
// Declare methods:
public void Update_Wave() {
// Get the mesh:
MeshFilter M_Filter = (MeshFilter)this.gameObject.GetComponent (typeof(MeshFilter));
MeshFilter C_Filter = (MeshFilter)this.gameObject.collider.GetComponent (typeof(MeshFilter));
Vector3[] Vertices = M_Filter.mesh.vertices;
// Adjust the mesh:
for(int A = 0; A < Vertices.Length; A++ ) {
Vertices[A].y = (float)(this.Pattern.Get_Position((decimal)Vertices[A].x + this.CurOffset));
}
// Update the mesh:
M_Filter.mesh.vertices = Vertices;
C_Filter.mesh.vertices = Vertices;
// Recalculate important information:
M_Filter.mesh.RecalculateNormals();
M_Filter.mesh.RecalculateBounds();
C_Filter.mesh.RecalculateBounds();
C_Filter.mesh.RecalculateNormals();
// Update the offset:
this.CurOffset += (decimal)this.Speed / 10000;
if( this.CurOffset > this.MaxOffset ) this.CurOffset = this.MinOffset;
}
// Declare extensions:
public void Update() {
this.Update_Wave();
}
public void Start() {
this.Pattern = new Wave((decimal)this.Amplitude, (decimal)this.Frequency);
// Get the offsets:
// Get the mesh:
MeshFilter M_Filter = (MeshFilter)this.gameObject.GetComponent (typeof(MeshFilter));
Vector3[] Vertices = M_Filter.mesh.vertices;
// Find the outer edges:
for(int A = 0; A < Vertices.Length; A++ ) {
if( (decimal)Vertices[A].x > (decimal)this.MaxOffset ) { this.MaxOffset = (decimal)Vertices[A].x; }
if( (decimal)Vertices[A].x < (decimal)this.MinOffset ) { this.MinOffset = (decimal)Vertices[A].x; }
}
this.CurOffset = this.MinOffset;
}
}
As you can see in the Update method I have already attempted to update said (collision)mesh (C_Filter), but have been unsuccessfull.
Any ideas, advice or suggestion on this?
So just to recap:
I have nice moving water with a not-too-high strain on the engine that has nice waves, but I need to update the collision mesh when I change the object mesh as well.
Note: I have already tried using Convex on that plane, and update it through there, without succes (at least for now).