Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Goubermouche · Sep 04, 2020 at 02:57 PM · pointdeformationboatoceanbuoyancy

height of a point in a mesh

hello!

I'm currently working on a buoyancy system and I need a way to get the water level height of a specific point (the transform of my boat) I tried to do It myself, however, I had no success

I currently have two scripts working together to create the ocean effect:

 [System.Serializable]
 public class Wave
 {
     public float noisewalkSpeed;
     public Vector3 scale = new Vector3(0.1f, 0.1f, 0.1f);
     public Vector3 speed = new Vector3(1.0f, 1.0f, 1.0f);
     public Vector3 waveDistance = new Vector3(1.0f, 1.0f, 1.0f);
     public Vector3 noiseStrength = new Vector3(1.0f, 1.0f, 1.0f);
     public Vector3 noiseWalk = new Vector3(1.0f, 1.0f, 1.0f);
     public float waveHeight;

 public Vector3 GetWave(Vector3 coord)
 {
     Vector3 xyz_coord1 = Vector3.zero;
     Vector3 xyz_coord2 = Vector3.zero;

     xyz_coord1.x += Mathf.Cos((Time.time * speed.x + coord.z) / waveDistance.x) * scale.x;
     xyz_coord1.x += Mathf.PerlinNoise(coord.y + noiseWalk.x, coord.z + Mathf.Sin(Time.time * 0.1f)) * noiseStrength.x;

     xyz_coord1.y += Mathf.Sin((Time.time * speed.y + coord.z) / waveDistance.y) * scale.y;
     xyz_coord1.y += Mathf.PerlinNoise(coord.x + noiseWalk.y, coord.z + Mathf.Sin(Time.time * 0.1f)) * noiseStrength.y;

     xyz_coord2.y += Mathf.Sin((Time.time * speed.y + coord.x) / waveDistance.y) * scale.y;
     xyz_coord2.y += Mathf.PerlinNoise(coord.x + noiseWalk.y, coord.z + Mathf.Sin(Time.time * 0.1f)) * noiseStrength.y;

     xyz_coord1.z += Mathf.Cos((Time.time * speed.z + coord.x) / waveDistance.z) * scale.z;
     xyz_coord1.z += Mathf.PerlinNoise(coord.x + noiseWalk.z, coord.y + Mathf.Sin(Time.time * 0.1f)) * noiseStrength.z;

     waveHeight = xyz_coord1.y + xyz_coord2.y;
     return xyz_coord1 + xyz_coord2;
 }

}

and

public class Ocean : MonoBehaviour {

 Mesh waterMesh;
 private Vector3[] newVertices;
 private Vector3[] originalVertices;
 public Wave waveScript = new Wave();
 public float height;

 // Use this for initialization
 void Start()
 {
     waterMesh = this.GetComponent<MeshFilter>().mesh;
     originalVertices = waterMesh.vertices;
 }

 // Update is called once per frame
 void Update()
 {
     moveSea();
     waveScript.noiseWalk.z += waveScript.noisewalkSpeed * Time.deltaTime;
 }

 void moveSea()
 {
     newVertices = new Vector3[originalVertices.Length];

     for (int i = 0; i < originalVertices.Length; i++)
     {
         Vector3 vertice = originalVertices[i];
         vertice = transform.TransformPoint(vertice);
         vertice += waveScript.GetWave(vertice);
         newVertices[i] = transform.InverseTransformPoint(vertice);
     }
     waterMesh.vertices = newVertices;
     waterMesh.RecalculateNormals();
 }

}

any advice would be greatly appreciated!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Glurth · Sep 04, 2020 at 04:40 PM

The hard way would be to use the oceans transform, to InverseTransform the boats position, putting it into the ocean mesh's local space. Then loop through all the ocean mesh vertices to find the one closest to the boat. Use the oceans transform to transform that vertex into world-space. Get the Y coordinate of that vertex for the ocean height. For more precision, you could find a FEW of the closest vertices on the mesh and compute the average height, weighted by the distance from the boat.


Simpler: Use a mesh collider on the ocean, and cast a ray(https://docs.unity3d.com/ScriptReference/Physics.Raycast.html) down from a safe distance over the boat's position . The RaycastHit result (https://docs.unity3d.com/ScriptReference/RaycastHit.html) contains the "hit point" which should be the surface of the ocean. (you may need to use tags to prevent a hit detection on the boat itself).


Simpliest: Adjust your wave function such that it provides the vertex coordinate itself, rather than an OFFSET (line 29) from the current coordinate. Then just call GetWave(boatPosition) to get the height of the ocean.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

135 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Having trouble with player boat movement with buoyancy pack 0 Answers

How do i make floating ship ? 0 Answers

Pro Water - Matching Rotation 0 Answers

Damping forces to stop boat oscillation 0 Answers

Making buoyancy script not mass based 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges