- Home /
Get plain coordinates for another object
Hello everyone,
I have a very interesting question that is boggling my mind. (I can't find reference or a book about this)
Preceived Outcome: Gameobject aka "Ship" is on the plain mesh "water" and waves come in that move the position/rotation of the ship to look 'realistic'.
Problem: I'm using "water" mesh filter changes shape and does not impact x,y,z. I'm using Mathf.Sin in a loop with baseheight[i] to get new values for mesh.RecalculateNormals() that creates the wave like motion.
How can I get the coordinates of the baseHeight and tell my ship it needs to raise or lower on the "water" plane?
//wave script Mesh mesh = GetComponent().mesh;
if (baseHeight == null)
baseHeight = mesh.vertices;
vertices = new Vector3[baseHeight.Length];
for (int i = 0; i < vertices.Length; i++)
{
Vector3 vertex = baseHeight[i];
vertex.y += Mathf.Sin(Time.time * speed + baseHeight[i].x + baseHeight[i].y + baseHeight[i].z) * scale;
vertices[i] = vertex;
}
mesh.vertices = vertices;
mesh.RecalculateNormals();
With using this on my "boat" Gameobject I can get a single point and make it raise/lower; therefore, this test script has shown me this way. Now to figure out how to angle the ship so it "rides" the waves.
foreach(Vector3 ocean_verts in sealevel.GetComponent().vertices) { if (transform.position.x < ocean_verts.x) { transform.position = new Vector3(transform.position.x, ocean_verts.y, transform.position.z); } }
Answer by Cornelis-de-Jager · Mar 23, 2018 at 05:18 AM
Use a RayCast from the ships position downward.
Then you can use in built functions for RayCastHit object to find the normal which I think is
Vector3 normal = hit.normal;
It looks like it keeps it consistently on 0 with this method (correct me if i'm wrong).
RaycastHit hit;
Physics.Raycast(this.transform.position, -Vector3.up, out hit, 0.5f);
Vector3 normal = hit.normal;
Debug.Log("raycast hits" + hit.normal);
transform.position = new Vector3(transform.position.x, normal.y, transform.position.z);
Gives this outcome:
Here are some of the vertices layed out on the plain. which unfortunately are not 0 and 1 all the time. :/
Hey sorry, I removed the distance of the raycast and now it consistently is giving 1's & 0's but nothing in between.
Hey, after having a closer look it seems your collider for your water mesh is not a mesh collider but rather a box collider. Try changing that.
You can't use a raycast in this case as it's impractical to update a meshcollider every frame. This would totally kill the performance. $$anonymous$$eshColliders do not update themselfs.
Answer by sentar · Mar 27, 2018 at 02:08 AM
This is the closest I could get with my current understanding. This is part of the script to create float on a ship object.
foreach(Vector3 ocean_verts in sealevel.GetComponent().vertices) {
if (transform.position.x < ocean_verts.x && transform.position.z < ocean_verts.z)
{
transform.position = new Vector3(transform.position.x, ocean_verts.y + 0.2f * Time.deltaTime, transform.position.z);
transform.rotation = Quaternion.Euler(-ocean_verts.y * Time.deltaTime, transform.localEulerAngles.y, ocean_verts.y * Time.fixedDeltaTime * sealevel.GetComponent<my_ocean>().scale * 360);
//transform.Rotate(-ocean_verts.y * Time.deltaTime, transform.localRotation.y, ocean_verts.z * Time.deltaTime);
}
if (transform.position.x < ocean_verts.x && transform.position.z > ocean_verts.z)
{
transform.position = new Vector3(transform.position.x, ocean_verts.y + 0.2f * Time.deltaTime, transform.position.z);
transform.rotation = Quaternion.Euler(-ocean_verts.y * Time.deltaTime, transform.localEulerAngles.y, ocean_verts.y * Time.fixedDeltaTime * sealevel.GetComponent<my_ocean>().scale * 360);
//transform.Rotate(-ocean_verts.y * Time.deltaTime, transform.localRotation.y, ocean_verts.z * Time.deltaTime);
}
if (transform.position.x > ocean_verts.x && transform.position.z > ocean_verts.z)
{
transform.position = new Vector3(transform.position.x, ocean_verts.y + 0.2f * Time.deltaTime, transform.position.z);
transform.rotation = Quaternion.Euler(-ocean_verts.y * Time.deltaTime, transform.localEulerAngles.y, ocean_verts.y * Time.fixedDeltaTime * sealevel.GetComponent<my_ocean>().scale * 360);
//transform.Rotate(-ocean_verts.y * Time.deltaTime, transform.localRotation.y, ocean_verts.z * Time.deltaTime);
}
if (transform.position.x > ocean_verts.x && transform.position.z < ocean_verts.z)
{
transform.position = new Vector3(transform.position.x, ocean_verts.y + 0.2f * Time.deltaTime, transform.position.z);
transform.rotation = Quaternion.Euler(-ocean_verts.y * Time.deltaTime, transform.localEulerAngles.y, ocean_verts.y * Time.fixedDeltaTime * sealevel.GetComponent<my_ocean>().scale * 360);
//transform.Rotate(-ocean_verts.y * Time.deltaTime, transform.localRotation.y, ocean_verts.z * Time.deltaTime);
}
}
If someone has a better method or a book/documentation i'm all ears.