• 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 Paxlord · Apr 15, 2017 at 11:58 AM · gameobjecttransform

How to get the height of a gameobject at a given point ?

Hi, I have a big gameObject which act as a big map.

I want to create an invisible TileMap for this object but I can't find a way to get the y coordinates at a random x and z coordinates.

I already know the function Terrain.SampleHeight(). Is there an equivalent for other gameObjects?

Thanks in advance.

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
0

Answer by Xarbrough · Apr 16, 2017 at 12:03 AM

Assuming "big gameObject" means a MeshRenderer and Mesh component, then you could use the Renderer.bounds property to get the bounding volume of a renderer object. You could also loop through Mesh.vertices, convert them to world space with Transform.TransformPoint and pick the highest y-value.

Comment
Add comment · Show 2 · 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 Paxlord · Apr 16, 2017 at 06:51 PM 0
Share

With these solutions, I can only get the maximum height of the game Object.

Here, I want to be able to give a X and a Z coordinates and get the height of the surface at this point.

I joined an image of my problem. How can I make all the red points return y=1, all the black ones return y=2 and all the blue ones return y=3?

Thanks.

alt text

image-explanation.png (245.4 kB)
avatar image Xarbrough Paxlord · Apr 16, 2017 at 08:42 PM 0
Share

If for any convenient reason you already have a meshcollider on your object you can raycast straight down onto the surface and measure the distance from a fixed high point. You can also loop through all vertices and find vertices close to the x and z coordinate, then pick one or average multiple to find a y-value that gives you the height of the object at that position in space.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to keep a Gameobject in the same position after a transform.Rotate? 2 Answers

Why does this wont work? transfor.find() 1 Answer

Are .gameObject and .transform both using GetComponent() in the background? 1 Answer

[CODE]Keep Gameobject position exactly at mouse cursor when cast float position to int 1 Answer

Convert a GameObject to a transform? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges