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 GlennNZ · Jul 22, 2014 at 11:21 AM · collisionlinecastspherecastline of sight

Spherecast/linecast in 2.5D (line of sight checks)

I have a 2.5D game world whereby the world is flat but I use a 3D camera angle to view it.

The world is populated with 2D complex polygons that are rendered at y=0 and have an associated collision mesh at y=0. I would like to calculate line of sight by casting a line at y=0 and checking if it intercepts any of the polygons.

I've tried a SphereCast and a LineCast and none seem to work if I try to cast with lines that have endpoints at y=0. If the end points of each cast is say, at y=10 and y=-10 then the collision is detected so it seems that the collision is only detected if going at a 3D orientation.

Does anyone have an alternative suggestion. I was using the following code.

 //p1.y = 10;
 //p2.y = -10;
 Vector3 dir = p2 - p1;
 int layerMask = 1 << LayerMask.NameToLayer("Obstacles");
 
 if (Physics.SphereCast(p1, 4.0f, dir, out hit, dir.magnitude, layerMask)) {
     Debug.Log ("*** NO LINE OF SIGHT ***");
 }
 else {
     Debug.Log ("*** CLEAR SHOT ***");
 }
Comment
Add comment · Show 2
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 robertbu · Jul 22, 2014 at 01:34 PM 0
Share

Why not use 2D colliders and 2D casts?

avatar image GlennNZ · Jul 22, 2014 at 11:59 PM 0
Share

Can 2D colliders be applied to a 3D world where up is the y direction and the 2D meshes are in the x,z plane? $$anonymous$$y world is 3D in that some of my y=0 planes are extruded into 3d (to represent buildings) but a lot are just planes. The colliders themselves are just 2D so it's cheaper to do mouse picking. I see in the Physics2D.Raycast docs http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html that the $$anonymous$$Depth and maxDepth refer to the z direction, but my meshes are in the x,z plane. I'm guessing because I haven't tried it yet, but I'd have thought that trying to make a PolygonCollider2D would result in a collider drawn in the x,y plane rather than the x,z plane. If that's the case, would the only solution be to rerender my entire world so that Up is actually in the z direction?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by GlennNZ · Jul 23, 2014 at 09:53 AM

I'm going to edit my previous answer and clarify what I've done for this which is...

  • Made a child GameObject for each of my GameObjects that contain the complex 2D polygons (that populate my 3D game world)

  • Added a PolygonCollider2D component to the child GameObject. By doing this, the parent GameObject can have a 3D Mesh Collider that can be used for other purposes such as with the A* library I'm using.

  • I assigned all child game objects with the PolygonCollider2D to a "Colliders2D" layer.

If you view the world in 2D scene view with the PolgyonCollider2D gizmos showing all the lines, you get to see the 2D world drawn perpendicular to the 3D world where Y is up.

With the above setup I can do a Physics2D.Linecast on the "Colliders2D" layer to determine collisions.

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

2 People are following this question.

avatar image avatar image

Related Questions

How do I get all colliders within a radius at a given point? 1 Answer

Get the name of the collided object 2 Answers

Lay a Sphere on a Point 3 Answers

Why is my spherecast returning an incorrect hit on mesh collider? 1 Answer

SphereCastAll return individual surface normals 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