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 BlueKickshaw · Mar 19, 2015 at 11:48 AM · c#raycastraycasting

[C# / Unity] Raycast Ignoring Self / Offsetting Instantiations?

Poorly phrased question, I know, but it really is the best way I can think to explain it at the moment.

Context:
The user can put up walls to create a box in the scene. In the empty state, a box is a non-functional room. A functional room is a zone extending out from a piece of furniture that defines the room, created when the user puts the room defining object down. For example, an empty box arrangement will become a bedroom when the user puts a bed down. What happens is the bed spawning instantiates a 'floor crawler'. The crawler checks what's around it in the four cardinal directions via rays. If it collides, it doesn't create an instance at that location.

You can see that happening here (Instead of having it all happen at once, I set it to advance a 'step' on key press): http://cdn.makeagif.com/media/3-19-2015/4Gc9Gl.gif

What you might be able to tell, is certain tiles are darker, because they have two crawler objects occupying the same space. The reason for this is that the crawlers don't have colliders until after they spawn another copy of themselves. My leading theory is that this occurs at the same time they're neighbours are repopulating, and it causes double over.

The reason they don't have colliders to begin with, is that the raycast was returning the gameobject as the collider, even when it was just the one starting crawler. How do I avoid this? I read about layer masks, I have no idea how to implement them in my scenario.

Code below: using UnityEngine; using System.Collections;

 public class FloorCrawl : MonoBehaviour {
     //Vector3 newPos;
     string _dir;
     public static int numC;
 //    LayerMask layermask = ~1 << IgnoreRaycast;
 
     void Update () {
         if (numC > 1000) {
             Destroy (gameObject);
         }
     }
 
     void Start () {
         CrawlStep ();
     }
 
     void CrawlStep(){
         if (numC<1000)
         for (int i=0; i<4; i++) {
 
             if (i==0){
                 {
 
                     _dir = "left";
                     RaycastHit2D _ray = Physics2D.Raycast (gameObject.transform.position, Vector3.left, 1);
                     if (!_ray) {
                         CreateCrawler (transform.position + Vector3.left);
                         numC++;
                     }
                 }
             }
             if (i==1){
                 {
                     _dir = "right";
                     RaycastHit2D _ray2 = Physics2D.Raycast (gameObject.transform.position, Vector3.right, 1);
                     if (!_ray2) {
                         CreateCrawler (transform.position + Vector3.right);
                         numC++;
                     }
                 }
             }
             if (i==2){
                 {
                     _dir = "up";
                     RaycastHit2D _ray3 = Physics2D.Raycast (gameObject.transform.position, Vector3.up, 1);
                     if (!_ray3) {
                         CreateCrawler (transform.position + Vector3.up);
                         numC++;
                     }
                 }
             }
             if (i==3){
                 {
                     _dir = "down";
                     RaycastHit2D _ray4 = Physics2D.Raycast (gameObject.transform.position, Vector3.down, 1);
                     if (!_ray4) {
                         CreateCrawler (transform.position + Vector3.down);
                         numC++;
                     }
                 }
             }
         }
         gameObject.AddComponent<BoxCollider2D> ();
     }
 
     void CreateCrawler(Vector3 newPos){
         GameObject currentFloorCrawler = Instantiate (Resources.Load ("Entities/Objects/_floorCrawler"), newPos, Quaternion.identity) as GameObject;
     }
 }
 
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

0 Replies

· Add your reply
  • Sort: 

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

Distribute terrain in zones 3 Answers

Raycast function doesn't work. 1 Answer

[C#] UNET Client raycast not the same as Server raycast 2 Answers

How do I single out GameObject being detected by Raycast in a destroy / respawn system 1 Answer

How to check if a raycast is not hitting any tagged colliders? 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