- Home /
Something wrong with the raycast
I've never asked a question here before but I'm really stumped so i thought i'd give this a shot.
I'm trying to make a dungeon generator for a rouguelike i've been working on, and i'm using my own method to generate the tiles and walls. the tiles and walls generate fine initially, using a raycast to check if blocks and tile are already there before placing new ones. When I try to fill in holes with more wall blocks, the code seems to mess up, and the raycast I set up is ignored. Basically, the walls generate whether there is a tile there or not.
the code is this: `using UnityEngine; using System.Collections;
public class wallscript : MonoBehaviour {
public Transform wallloc;
public GameObject wall;
public float radius;
// Use this for initialization
void Start () {
StartCoroutine(mycoroutine());
}
IEnumerator mycoroutine(){
yield return new WaitForSeconds(7);
wallfill();
yield return new WaitForSeconds(9);
wallborders();
}
void wallfill(){
if(!Physics.Raycast(wallloc.position, Vector3.right, 1)){
Debug.Log ("good");
Instantiate(wall,wallloc.position+(wallloc.right), wallloc.rotation);
}
}
void wallborders(){
}
}'
the debug log even tells me there is nothing to the right of the wall, even though there is a tile already there. Does anyone have any idea as to why this is?
Thanks in advance!!
In wallfill() add
Debug.Log(wallloc.position);
to see what the actual transform is just before the Raycast
@ meat5000 I get a lot of numbers by doing that, since there's many wall blocks in the stage. They're not very coherent. I don't see much of a pattern.
Well there's your problem :) wallloc.position should be completely coherent if you are trying to find what is 1 unit to the right of it.
It should give you three numbers; x, y and z coordinates.
Point is with
void wallfill(){
Debug.Log(wallloc.position);
if(!Physics.Raycast(wallloc.position, Vector3.right, 1)){
you should be able to deter$$anonymous$$e which space or block the Raycast is shooting at and see for yourself it is correct.
Answer by OP_toss · Nov 20, 2013 at 10:50 PM
You raycast to the world-space right direction (Vector3.right), but then you instantiate to the local wallloc.right direction. These are 2 different values that depend on the rotation of wallloc. If wallloc is rotated, this seems like it could cause problems.
Also, please use camel case. I stared at "wallloc" for a while until I understood it was "wall" + "loc".
Also it seems like since you have this in a coroutine, that you want it to build multiple walls iteratively. Does that mean you meant to move "wallloc" after instantiating a wall? As it is, wallloc doesn't move, so it should keep checking the same spot and keep instantiating at the same spot. Maybe we're not seeing all the code involved?
Hope that helped!
I tried changing the instantiation so it matches the world space of the raycast, but no dice.
I had a theory that the raycast is either missing the tiles because they're so low, or the raycast only reads the wall blocks and not the tiles.
and sorry about forgetting the camel case, i'm relatively new to program$$anonymous$$g, and it completely slipped my $$anonymous$$d.
Your answer
Follow this Question
Related Questions
How can i modify a float in another script in C#? 1 Answer
Distribute terrain in zones 3 Answers
Automatic Node Connection - Help 1 Answer
How to select an object with TOUCH and change its animation 2D 1 Answer
Multiple Cars not working 1 Answer