Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 sentar · Mar 23, 2018 at 05:05 AM · coordinatesmeshfiltermesh verticesmesh manipulationmesh-deformation

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();

alt text

mesh-to-boat-error.png (295.2 kB)
Comment
Add comment · Show 1
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
avatar image sentar · Mar 23, 2018 at 05:55 AM 0
Share

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); } }

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
Comment
Add comment · Show 6 · 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
avatar image sentar · Mar 23, 2018 at 06:03 AM 0
Share

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: alt text

floating-boat-on-mesh-error.png (221.0 kB)
avatar image sentar · Mar 23, 2018 at 06:05 AM 0
Share

Here are some of the vertices layed out on the plain. which unfortunately are not 0 and 1 all the time. :/

alt text

wave-vertices.png (19.7 kB)
avatar image sentar · Mar 23, 2018 at 06:16 AM 0
Share

Hey sorry, I removed the distance of the raycast and now it consistently is giving 1's & 0's but nothing in between.

avatar image Cornelis-de-Jager sentar · Mar 23, 2018 at 06:22 AM 0
Share

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.

avatar image sentar · Mar 23, 2018 at 10:41 PM 0
Share

I thought that originally but this is a mesh collider

alt text

water-plane-collider.png (297.8 kB)
avatar image Bunny83 · Mar 23, 2018 at 11:47 PM 0
Share

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.

avatar image
0

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);
            }
     }
Comment
Add comment · Show 1 · 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
avatar image sentar · Mar 27, 2018 at 02:09 AM 0
Share

If someone has a better method or a book/documentation i'm all ears.

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

77 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

Related Questions

How to manipulate(flatten) the Mesh of an object 0 Answers

Problem with setting vertices positions and smoothing groups. 1 Answer

How can i generate my mesh to get a mesh for 2d light? 0 Answers

Sort a list based on edge connections 1 Answer

Splitting shared vertices in a plane 0 Answers


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