Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Tibelius · Aug 21, 2013 at 07:37 AM · collisionphysicsgamerealtimeheight

How to get the height of a tower made out of blocks?

So I'm making this sort of a 3D tetris game where the main point is to create as tall towers as possible. The data I need from the towers is the height and position. At the point of the tower the tower creates an influence radius, where the player is able to place more blocks. At the moment I have my game board where players drop pieces and I have two different block prefabs at the moment. Straight line block with one box collider childed to it and L-block with 2 box colliders respectively.

Now the problem is I can't figure out how to get the height of a tower. And I need to be able to get the bottom blocks position to get the point where the tower is. The tower height should be the absolute highest point measured in a 90 degree angle from the bottom block to the tip of the tower.

So here the middle tower should be the highest. and the third one should be a bit higher than the first one as the block is a bit diagonally and reaches higher. Block count does not affect the outcome. Only the height.

Should I do something with some sort of child structure? So that every time you stack a block it goes in to a tower hierarchy? Or maybe try to get a height map and locate the tower from that?

I really don't know how I would do either of those, but those solutions sound somehow logical maybe... I would be really glad if someone atleast points me to the right direction. Almost forgot: I use C# for my scripts if that matters to you all.

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

Answer by fafase · Aug 21, 2013 at 08:34 AM

Let's consider a scenario.

I have a box of x = 5, y = 1, z is not relevant to our issue. If I use collider.bounds.extent I get (2.5f,0.5f,z) since those are half of the size. I could also simply store the values in a script.

I am about to give you a solution but you could maybe optimize it and mostly this is for one object, you still to figure it out over the whole game:

Here we go.

  1. Get the vertices of the model

  2. Convert vertices to world position

  3. Find the highest y value of the vertices

  4. Use projection to find the vector from the highest point to the ground

  5. Get the distance with magnitude.

      Vector3 [] vertices;
         Vector3[] _meshPos;
     
         void Start(){
             Mesh mesh = GetComponent<MeshFilter>().mesh; 
             vertices = mesh.vertices;   // 1
             _meshPos = new Vector3[vertices.Length];
         }
         
         public void Update(){
             // 2
             for(int i = 0; i< vertices.Length;i++){
                 _meshPos[i] = transform.TransformPoint(vertices[i]);
             }
                 //3
             Vector3 highest = FindHighestPoint(_meshPos);
             // 4
             Vector3 projection = Vector3.Project( -highest, Vector3.down);
                 // 5
             float distance = projection.magnitude;
             print (distance);
         }
         
         public Vector3 FindHighestPoint (Vector3[] arr)
         {
             Vector3 highest = Vector3.zero;
             float y = 0;
             foreach (Vector3 vec in arr)
             {    
                 if(vec.y > y){
                     
                     y = vec.y;
                     highest = vec;
                 }
             }
             return highest;
         }
    
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 Tibelius · Aug 21, 2013 at 09:14 AM 0
Share

This is more than enough to get me started. I think I'll make some kind of a collider script to identify the bottom block and to create a tower. Then put all connecting blocks' highest points to a list and choose the largest value from them..... Or something :D

Thank you for a brilliant answer in any case!

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

15 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

Related Questions

I Want a ring to move to wire from inside...,I want To move a ring on wire? 2 Answers

How to transfer bat's transnational and angular velocity to ball to simulate realistic ball movement. 0 Answers

How to avoid a gameobject inside another while they're in collision? 0 Answers

Normal vector from collision? 1 Answer

Bump when crossing box colliders 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