- Home /
Question by
Shadowskull1247 · Jul 14, 2017 at 06:59 AM ·
javascriptinstantiateraycast
Raycast help?
I am trying to create a procedural and randomly generated city. It works nearly perfectly, but it tends to overlap a LOT. I am trying to use a raycast to determine if there is enough space for a road/building to spawn before it spawns anything, but it is not working. What am I doing wrongly?
Code:
#pragma strict
public var Crossroad: GameObject;
public var Road: GameObject;
public var HorizontalRoad: GameObject;
public var HorizontalRoadDown: GameObject;
public var DisabledRoad: GameObject;
public var Lamp: GameObject;
var CityLimitX1 = 500;
var CityLimitZ1 = 500;
var CityLimitX2 = -500;
var CityLimitZ2 = -500;
var i = 0;
var Distance : float;
static var StartCol: boolean = true;
//var Number = Random.Range(1, 16);
//static var CoordX =
//}
function Start () {
//var Start = true;
var CurrentXPos = transform.position.x;
var CurrentZPos = transform.position.z;
var CrossroadNX = transform.position.x + 90;
var TurnNX = transform.position.x + 60;
var RoadNX = transform.position.x + 30;
var Cross3wayNX = transform.position.x + 25;
var CrossroadNZ1 = transform.position.z + 30;
var TurnNZ = transform.position.z + 60;
var RoadNZ = transform.position.z + 20;
var Cross3wayNZ = transform.position.z + 30;
var RandomNum = Random.Range(1, 5);;
//var hit : RaycastHit;
//var SphereVar = new Vector3(CurrentXPos, 0, CurrentZPos);
//if (Physics.SphereCast(SphereVar, 30, transform.forward, hit, 30)) {
var hit : RaycastHit;
//var fwd: Vector3 = transform.TransformDirection(Vector3.forward);
//if (Physics.Raycast(transform.position, fwd, 1000)) {
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit)) {
Debug.Log("There is something in front of the object!");
Debug.Log("Failed.");
Distance = hit.distance;
}
//for (var i: int = 0; i < 10; i++) {
if (RandomNum == 1) {
if (CrossroadNX < CityLimitX1) {
if (CrossroadNZ1 < CityLimitZ1) {
if (CrossroadNX > CityLimitX2) {
if (CrossroadNZ1 > CityLimitZ2) {
if (Distance > 90) {
Debug.Log("Fired Crossroad");
Instantiate(Crossroad, new Vector3(CurrentXPos + 60, 0, CurrentZPos), Quaternion.Euler(Vector3(-90, 90, 0)));
Instantiate(DisabledRoad, new Vector3(CurrentXPos + 30, 0, CurrentZPos), Quaternion.Euler(Vector3(-90, 90, 0)));
Instantiate(HorizontalRoad, new Vector3(CurrentXPos + 60, 0, CurrentZPos + 30), Quaternion.Euler(Vector3(-90, 0, 0)));
Instantiate(HorizontalRoadDown, new Vector3(CurrentXPos + 60, 0, CurrentZPos - 30), Quaternion.Euler(Vector3(-90, 0, 0)));
Instantiate(Road, new Vector3(CurrentXPos + 90, 0, CurrentZPos), Quaternion.Euler(Vector3(-90, 90, 0)));
//Destroy(GetComponent(SecondCityGen));
GetComponent(SecondCityGen).enabled = false;
Debug.Log("Failed to exit script");
}
}
}
}
}
}
//}
if (RandomNum == 2) {
if (RoadNX < CityLimitX1) {
if (RoadNZ < CityLimitZ1) {
if (RoadNX > CityLimitX2) {
if (RoadNZ > CityLimitZ2) {
if (Distance > 30) {
Debug.Log("Fired Road1");
Instantiate(Road, new Vector3(CurrentXPos + 30, 0, CurrentZPos), Quaternion.Euler(Vector3(-90, 90, 0)));
//Destroy(GetComponent(SecondCityGen));
GetComponent(SecondCityGen).enabled = false;
Debug.Log("Failed to exit script");
}
}
}
}
}
}
if (RandomNum == 3) {
if (RoadNX < CityLimitX1) {
if (RoadNZ < CityLimitZ1) {
if (RoadNX > CityLimitX2) {
if (RoadNZ > CityLimitZ2) {
if (Distance > 30) {
Debug.Log("Fired RoadLamp");
Instantiate(Road, new Vector3(CurrentXPos + 30, 0, CurrentZPos), Quaternion.Euler(Vector3(-90, 90, 0)));
Instantiate(Lamp, new Vector3(CurrentXPos + 30, 0, CurrentZPos + 6), Quaternion.Euler(Vector3(0, 180, 0)));
Instantiate(Lamp, new Vector3(CurrentXPos + 30, 0, CurrentZPos - 6), Quaternion.Euler(Vector3(0, 0, 0)));
//Destroy(GetComponent(SecondCityGen));
GetComponent(SecondCityGen).enabled = false;
Debug.Log("Failed to exit script");
}
}
}
}
}
}
if (RandomNum == 4) {
if (RoadNX < CityLimitX1) {
if (RoadNZ < CityLimitZ1) {
if (RoadNX > CityLimitX2) {
if (RoadNZ > CityLimitZ2) {
if (Distance > 30) {
Debug.Log("Fired Road3");
Instantiate(Road, new Vector3(CurrentXPos + 30, 0, CurrentZPos), Quaternion.Euler(Vector3(-90, 90, 0)));
//start = false;
GetComponent(SecondCityGen).enabled = false;
Debug.Log("Failed to exit script");
}
}
}
}
}
}
}
function Update () {
if (StartCol == false) {
var TempCol : MeshCollider;
TempCol = GetComponent.<MeshCollider>();
TempCol.isTrigger = false;
Destroy(GetComponent(SecondCityGen));
}
}
function OnTriggerEnter (other : Collider) {
for (var T: int = 0; T < 50; T++) {
if (StartCol == true) {
Destroy(gameObject);
}
}
}
Comment
Answer by TWicked · Jul 14, 2017 at 08:13 AM
Try using Physics.OverlapSphere to see if the place where you are placing object is empty instead of using raycast.
https://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612130030im_/https://answers.unity.com/themes/thub/images/avi.jpg)