• 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
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

People who like this

0 Show 0
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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Can't find a clone object with RayCast? [Solved] 2 Answers

Help finding out which side of a cube a raycast hits 1 Answer

Instantiate an object at hit position without it intersecting anything 1 Answer

raycasting without colliders 0 Answers

RayCasting Mouse 2 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