- Home /
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" :).
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
If you could post the code you're using that would be good.
I added the image, that how i want it to basically spread.
Answer by Joshua · Jul 08, 2011 at 10:40 AM
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.
thanks I havent seen, that I dont have the ! at the other 3 Raycasts...ill try it
Nope...what is it doing is, that the first cube places another under itself and does it to infinite... like places the same block at the same place nothing else...
Well to be honest I don't like the logic setup of how you do it. Don't do this in the Update() function because you want it run once per cube, not every frame for every cube. Second of all, your coding this from the perspective of the cubes, code it form the perspective of a masterscript, then use for and while loops to iterate trough all the blocks and add to them.
reason why i have it in update function is, that it will be water, that is flowing... well yeah, i could place it into start function , or use a masterscript... ill try something out...
Well you can keep checking it continuously, but that will get quite expsive pretty soon with your current setup. For that a masterscript iterating through them all every (few) frames would also be better.