Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Arctic_Evolution · Mar 02, 2018 at 11:45 PM · pysics

Expert question - I want to find out the vector of the lowest vertex from the centre point of an object

Any Object. Mark it in the centre. Bee Line arrow down. Hit that final vertex. Get the vector point.

Then I want to raycast down from that vector.

So this would be the edge of an object, down from the centre to the bottom lowest y of the object.

Why am I do I need this. Ok colliders arnt working for me to achieve the physics I want. I need a centre point like this to detect things.

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
Best Answer

Answer by Xarbrough · Mar 03, 2018 at 02:03 AM

Here is some code to help you out:

 using UnityEngine;
 
 public class MyScript : MonoBehaviour 
 {
     public MeshFilter meshFilter;
 
     private Vector3 lowestVertex;
     private Vector3 center;
 
     private void Start()
     {
         if(meshFilter == null || meshFilter.sharedMesh == null)
             return; // TODO: Error feedback.
         
         lowestVertex = FindLowestVertex();
         center = FindCenter();
     }
 
     private Vector3 FindLowestVertex()
     {
         float lowestYCoordinate = float.PositiveInfinity;
         var lowestVertex = meshFilter.transform.position; // Some sensible default.
 
         var vertices = meshFilter.sharedMesh.vertices;
         for (int i = 0; i < vertices.Length; i++) 
         {
             // Check for the lowest position in world space.
             Vector3 worldCoord = meshFilter.transform.TransformPoint(vertices[i]);
             if(worldCoord.y < lowestYCoordinate)
             {
                 lowestYCoordinate = worldCoord.y;
                 lowestVertex = meshFilter.transform.InverseTransformPoint(worldCoord);
             }
         }
         return lowestVertex; // In local space.
     }
 
     private Vector3 FindCenter()
     {
         Vector3 center = meshFilter.transform.position;
         Vector3 sum = Vector3.zero;
         var vertices = meshFilter.sharedMesh.vertices;
         for (int i = 0; i < vertices.Length; i++) 
         {
             sum += vertices[i];
         }
         center = sum / vertices.Length;
         return center;
     }
 
     private void FixedUpdate()
     {
         Vector3 centerWorld = meshFilter.transform.TransformPoint(center);
 
         Debug.DrawLine(
             centerWorld + new Vector3(0, -0.1f, 0), 
             centerWorld + new Vector3(0, +0.1f, 0), 
             Color.red);
         
         Debug.DrawLine(
             centerWorld + new Vector3(-0.1f, 0, 0), 
             centerWorld + new Vector3(+0.1f, 0, 0), 
             Color.red);
 
         Vector3 origin = meshFilter.transform.TransformPoint(lowestVertex);
         Debug.DrawRay(origin, Vector3.down, Color.green);
         // TODO: Raycast
     }
 }
 

"Any object" is a little too broad, so I assumed you were talking about meshes. If you don't use the transform as a pivot, you can take the average of all vertices to find a center position.

To find the lowest vertex position in a mesh you have to loop through all the vertices and compare each one.

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 Arctic_Evolution · Mar 03, 2018 at 02:59 AM 0
Share

Yes, I knew it involved the mesh, but referred in the question as context of the object. Very happy with this response. Thanks.

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

75 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

Related Questions

Car Movment Script not fully working!!!!!!! 1 Answer

(at very high altitude many objects are created)How to make the object broke from falling answer 1 Answer

Performing if loop multiple times to perform attack animation 1 Answer

Basics on 2d gameplay for a 2.5d Game 1 Answer

Pysics not working as expected 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