Raycasting

Hey guys! I don’t have a clue what am I doing wrong…Simply. I need to raycast into the Z direction, than if there is nothing I need to instantiate a cube and if there is something I need to check the Y and -Y and X and -X and if there is nothing again create cubes at the sides…I did raycasting like XY times…but I just can’t figure this one out. And that cubes are the same so they will again check the same thing, so it will something like “spread” :).

Spreading

var prefab : Transform;

var rayRange : float = 2;
var mask : LayerMask;

var canPlace : boolean;

var right : boolean;
var left : boolean;
var up : boolean;
var down : boolean;
var someThingIsBelow : boolean = true;

function Start ()
{
    prefab = transform;
}

function Update ()
{
    Bottom();
	Place();
}
function Bottom()
{
	var hit : RaycastHit;
	
	if(Physics.Raycast(transform.position, -transform.forward, hit, rayRange))
		if(hit.collider.tag != "waterBlock")
		{
			someThingIsBelow = true;
			canPlace = true;
			CheckSides();
		}
		else
			canPlace = false;
	else
	{
		someThingIsBelow = false;
		canPlace = true;
	}
}

function CheckSides()
{
	if(canPlace)
	{
		var sideHit : RaycastHit;
		
		if (Physics.Raycast(transform.position, transform.up, sideHit, rayRange)) up = false;
		else up = true;

		if (Physics.Raycast(transform.position, -transform.up, sideHit, rayRange)) down = false; 	
		else down = true;
		
		if (Physics.Raycast(transform.position, transform.right, sideHit, rayRange)) right = false;
		else right = true;
		
		if (Physics.Raycast(transform.position, -transform.right, sideHit, rayRange)) left = false;
		else left = true;
	}
}

function Place()
{
	if(canPlace)
	{
		if(!someThingIsBelow)
			DoBlock(Vector3(0,0,1));
		
		if(someThingIsBelow)
			if(right) DoBlock(Vector3(1,0,0));
			if(left) DoBlock(Vector3(-1,0,0));
			if(up) DoBlock(Vector3(0,1,0));
			if(down) DoBlock(Vector3(0,-1,0));	
	}	
}

function DoBlock(offset : Vector3)
{
    var myPos = transform.position;
    Instantiate(prefab, myPos + offset, transform.rotation);
}

– David

First of all, transform.TransformDirection( Vector3.forward ) is the same as transform.forward, same goes for up and right. Second of all in your if( tag != “waterblock”) block of code you check the first raycast if there is not something above it, but then for underneath and next to you check if there is something. Add a ‘!’. And lastly, you write

var myPos = Vector3(transform.position.x,transform.position.y,transform.position.z);

where you could just write

var myPos = transform.position;

Fix the raycasting and see if that fixes it.