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
0
Question by Bennett-Lynch · Jan 30, 2015 at 11:21 PM · geometrygame objecttop

Finding farthest point on top of game object (geometry)

In the below scene, I am trying to calculate the farthest point from the camera at the top of each cylinder (designated roughly by the green dots):

alt text

Assuming the object's height and radius are known, and the transform position of both the object and the camera, what is the most efficient way to do this?

This is what I came up with:

 float xDiff = transform.position.x - Camera.main.transform.position.x;
 float zDiff = transform.position.z - Camera.main.transform.position.z;
 float angle = Mathf.Atan2( xDiff, zDiff );
 
 float zAdjust = Mathf.Cos( angle ) * radius;
 float xAdjust = Mathf.Sin( angle ) * radius;
 
 barPos = Camera.main.WorldToScreenPoint( new Vector3( transform.position.x + xAdjust, transform.position.y + height, transform.position.z + zAdjust ) );

But it seems to have a somewhat heavy hit on my frame rate. I still have some other kinks in my code I need to work out, so I'm not even sure if my geometry is 100% correct, but if it's going to be too expensive to update every frame I figured I should seek a better alternative. Any ideas?

Comment
Add comment · Show 4
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 PhoenixBlackReal · Jan 31, 2015 at 11:24 AM 0
Share

If you are looking for the furthest visible point and the mesh is not too complex, I would suggest such a solution: 1. http://docs.unity3d.com/ScriptReference/$$anonymous$$esh.html Get all the vertices of the mesh. 2. use something like Vector3 worldPt = transform.TransformPoint(meshVert); to get the world coordinate. 3. $$anonymous$$nowing the coordinates of the all the vertices is enough to find out which is the furthest point. If however, you need the furthest visible one - I'd suggest shooting rays at every vertex. Depending on intersection and hit.point you could find out the furthest visible one. It's one way, but it's pretty expensive. Hope it helps in any way!

avatar image Bennett-Lynch · Jan 31, 2015 at 06:10 PM 0
Share

This looks promising, I'll check it out and get back to you. Do you think the raycasting is more expensive than exa$$anonymous$$ing all of the vertices? If so, I can probably skip that step and just look for a vertex that is as far as possible with a height equal to the object's height.

avatar image PhoenixBlackReal · Jan 31, 2015 at 08:56 PM 0
Share

Raycasting is rather expensive, I would not use it if the model is moving or has lots of vertices.

avatar image DESIMA · Jan 31, 2015 at 09:19 PM 0
Share

but if you only need to know the distance in the start of per click or something then its ok doing raycasting. But yes if you run it in an Update() method its gonna suck resources

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DESIMA · Jan 31, 2015 at 04:53 PM

It seems like you just want the longest distance to all you points or transforms.

if so then get a list of your transforms, and for each object do

 //get the length of the vector
 (obj.position - camera.posion).length

maybe throw and Math.Abs on it depending on the direction

you can then save and compare to next length

 float maxDist = Mathf.Max(maxDist, (obj.position - camera.posion).length);

then at the end of the loop you have maxDist.

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 Bennett-Lynch · Jan 31, 2015 at 06:05 PM 0
Share

All this is going to do is find the distance between the camera and the center of each cylinder though (correct me if I'm wrong). I'm trying to find the point at the top of each cylinder that is farthest from the camera.

avatar image DESIMA · Jan 31, 2015 at 06:14 PM 0
Share

no you are right, the transforms are the center of the ojbect. If you need the top, one way is to add an empty gameobject and place it on the top by your self, then use that gameobject as the transform in the calculation.

or if you add colliders on the objects you can use collider.bounds, to get the distance offset

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Most efficient way to create cubes with multiple textures 2 Answers

Too much triangles and more doubts performance-related 1 Answer

geometry how to find ray local hit point? 1 Answer

How to get random point on the perimeter of a rectangle? 1 Answer

Call different geometry shader functions, for different primitive type: How? 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