Search Unity

Question Anyone know how to get height at location of this script LowPolyWater.

Discussion in 'Scripting' started by leonardomenna1999, Apr 21, 2021.

  1. leonardomenna1999

    leonardomenna1999

    Joined:
    Jan 13, 2021
    Posts:
    1
    im trying to get wave height at any given point form this script, but i dont know how.

    Script:

    using UnityEngine;


    public class LowPolyWater : MonoBehaviour
    {
    public float waveHeight = 0.5f;
    public float waveFrequency = 0.5f;
    public float waveLength = 0.75f;

    //Position where the waves originate from
    public Vector3 waveOriginPosition = new Vector3(0.0f, 0.0f, 0.0f);

    MeshFilter meshFilter;
    Mesh mesh;
    Vector3[] vertices;
    Vector3[] c;

    public static LowPolyWater instance;


    private void Awake()
    {
    //Get the Mesh Filter of the gameobject
    meshFilter = GetComponent<MeshFilter>();


    if (instance == null)
    {
    instance = this;
    }
    else if (instance != this)
    {
    Debug.Log("istanza gia esistente");
    Destroy(this);
    }
    }

    void Start()
    {
    CreateMeshLowPoly(meshFilter);
    }

    /// <summary>
    /// Rearranges the mesh vertices to create a 'low poly' effect
    /// </summary>
    /// <param name="mf">Mesh filter of gamobject</param>
    /// <returns></returns>
    MeshFilter CreateMeshLowPoly(MeshFilter mf)
    {
    mesh = mf.sharedMesh;

    //Get the original vertices of the gameobject's mesh
    Vector3[] originalVertices = mesh.vertices;

    //Get the list of triangle indices of the gameobject's mesh
    int[] triangles = mesh.triangles;

    //Create a vector array for new vertices
    Vector3[] vertices = new Vector3[triangles.Length];

    //Assign vertices to create triangles out of the mesh
    for (int i = 0; i < triangles.Length; i++)
    {
    vertices = originalVertices[triangles];
    triangles = i;
    }

    //Update the gameobject's mesh with new vertices
    mesh.vertices = vertices;
    mesh.SetTriangles(triangles, 0);
    mesh.RecalculateBounds();
    mesh.RecalculateNormals();
    this.vertices = mesh.vertices;

    return mf;
    }

    void Update()
    {
    c= GenerateWaves();
    }

    /// <summary>
    /// Based on the specified wave height and frequency, generate
    /// wave motion originating from waveOriginPosition
    /// </summary>
    public Vector3[] GenerateWaves()
    {
    for (int i = 0; i < vertices.Length; i++)
    {
    Vector3 v = vertices;

    //Initially set the wave height to 0
    v.y = 0.0f;

    //Get the distance between wave origin position and the current vertex
    float distance = Vector3.Distance(v, waveOriginPosition);

    distance = (distance % waveLength) / waveLength;

    //Oscilate the wave height via sine to create a wave effect
    v.y = waveHeight * Mathf.Sin(Time.time * Mathf.PI * 2.0f * waveFrequency
    + (Mathf.PI * 2.0f * distance));


    //Update the vertex
    vertices = v;
    }




    //Update the mesh properties
    mesh.vertices = vertices;
    mesh.RecalculateNormals();
    mesh.MarkDynamic();
    meshFilter.mesh = mesh;

    return vertices;

    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I was wondering if that would work too, but does a MeshCollider update it's geometry in real time when the underlying mesh changes?

    You should be able to just look at the MeshCollider in the Scene View during play mode to find out.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Pretty sure it simply uses whatever mesh it "finds" when it looks in its
    .sharedMesh
    .

    I base this assumption on this link:

    https://docs.unity3d.com/ScriptReference/MeshCollider-sharedMesh.html

    And specifically these words:

    "The mesh object used for collision detection."

    I assume the second line of the above applies mostly if you tick the .convex box, as obviously a separate convex shell must be made at some point.
     
    Joe-Censored likes this.