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 Disaster · Jan 08, 2012 at 01:32 PM · raycastrtsbounds

Checking if ground is 'level'

I am attempting to create RTS style "structure" placement on a surface and I need to check if the ground area below the structure is level. In order to do this, I am casting rays from 9 points around the structures 'bounds' and averaging the hit normals to ensure the ground can be considered level. In order to do that, I am having to create an array of 9 points like so:

 Vector3 lpos = new Vector3(_tr.position.x - _groundMarker.renderer.bounds.extents.x, _tr.position.y, _tr.position.z);
 Vector3 rpos = new Vector3(_tr.position.x + _groundMarker.renderer.bounds.extents.x, _tr.position.y, _tr.position.z);
 Vector3 tlpos = new Vector3(_tr.position.x - _groundMarker.renderer.bounds.extents.x, _tr.position.y, _tr.position.z - _groundMarker.renderer.bounds.extents.z);
 Vector3 trpos = new Vector3(_tr.position.x + _groundMarker.renderer.bounds.extents.x, _tr.position.y, _tr.position.z + _groundMarker.renderer.bounds.extents.z);
 Vector3 blpos =  new Vector3(_tr.position.x - _groundMarker.renderer.bounds.extents.x, _tr.position.y, _tr.position.z + _groundMarker.renderer.bounds.extents.z);
 Vector3 brpos =  new Vector3(_tr.position.x + _groundMarker.renderer.bounds.extents.x, _tr.position.y, _tr.position.z - _groundMarker.renderer.bounds.extents.z);
 Vector3 tmpos = new Vector3(_groundMarker.renderer.bounds.center.x, _tr.position.y, _tr.position.z + _groundMarker.renderer.bounds.extents.z);
 Vector3 bmpos = new Vector3(_groundMarker.renderer.bounds.center.x, _tr.position.y, _tr.position.z - _groundMarker.renderer.bounds.extents.z);
 Vector3 cpos = _groundMarker.renderer.bounds.center;

I am having to update these vectors every time the ground marker changes scale (depending on the size of the structure) and it just strikes me as kind of messy. Is there a better way to do this without having to cast 9 individual rays? It's be nice if I could simplify this code right down. Any suggestions?

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

Answer by Owen-Reynolds · Jan 09, 2012 at 04:34 AM

Raycasting is when you aren't sure what might be down there -- maybe the ground, or a rock collider, or a platform... . If you know you want to check the ground, can just look it up:

 float HT = Terrain.activeTerrain.SampleHeight( lpos );
 // lpos.y is ignored, but has to be set

If you want to pretty up the code, could use loops to find the 9 points. Won't be any less work for the computer:

 Vector3[] P = new Vector3[9];
 
 for(int xp=-1; xp<=1; xp++) // xp and zp go through every combination
 for(int zp=-1; zp<=1; zp++) // of (-1,0) (1,1) ...
   P[xp*3+zp] = new Vector3( xp*extents.x, 0, zp*extents.z);
Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to move objects/units around [RTS] 1 Answer

Raycasting to a GameObject's Bounds 1 Answer

Math/Programming Question 4 Answers

How do I instantiate an object in an RTS style game? 1 Answer

Can you find the length or “bounds” of an edge collider2D using Bounds? 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