- Home /
[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;
}
}
Your answer
Follow this Question
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