Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Shadowskull1247 · Aug 11, 2017 at 06:49 AM · javascriptinstantiateraycasthit detection

Issue with Raycast hit.distance Variables Reseting Itself

Alright. I am making a procedural and randomly generated city and I am trying to eliminate road/building overlaps by using Raycasts to determine if the next generated piece of road is going to overlap with anything, and if it is, stop building that street and put a dead end piece, but the problem I am having is that my generator will only build one road, then return that that there is no more room for another road piece, although there is absolutely nothing in the way. I even put a debug log in the earlier part of the script, and it shows that it has enough room for it, but it wont build anything. And the odd part is that some times, it will build the road pieces, but most of the time, it wont. I figured that maybe my raycasts are facing the wrong direction, but nope.

alt text

And before you tell me that there is nothing for them to collide on, yes there is. I put four box colliders around the map.

So, what i am thinking is hit.distance resets itself for some reason. No idea why, but Im grasping at straws here. So, I tried redeclaring the variable that holds the hit.distance variable and putting the Raycast in the Update function. that didnt work either. Please help me?

 #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, 4);
 var UpDebug = transform.position.y + 20;
 
 //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.down);
 Debug.DrawRay(transform.position, fwd * 99999, Color.green, 999999);
     if (Physics.Raycast(transform.position, fwd, hit)) {
 
     //if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit)) {
         //Debug.Log("There is something in front of the object!");
         //transform.TransformDirection(Vector3.forward)
         Distance = hit.distance;
 
         Debug.Log(Distance + " " + "From Normal");
 }
     //for (var i: int = 0; i < 10; i++) {
     if (RandomNum == 1) {
         if (CrossroadNX < CityLimitX1) {
             if (CrossroadNZ1 < CityLimitZ1) {
                 if (CrossroadNX > CityLimitX2) {
                     if (CrossroadNZ1 > CityLimitZ2) {
                     Debug.Log(Distance + " " + "Attempt Crossroad Fire");
                     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) {
                     Debug.Log(Distance + " " + "Attempt Road1 Fire");
                     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) {
                     Debug.Log(Distance + " " + "Attempt RoadLamp Fire");
                     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) {
                     Debug.Log(Distance + " " + "Attempt Road3 Fire");
                     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 == true) {
 var hit : RaycastHit;
 var fwd: Vector3 = transform.TransformDirection(Vector3.down);
 Debug.DrawRay(transform.position, fwd * 99999, Color.green, 999999);
     if (Physics.Raycast(transform.position, fwd, hit)) {
 
     //if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit)) {
         //Debug.Log("There is something in front of the object!");
         //transform.TransformDirection(Vector3.forward)
         Distance = hit.distance;
         Debug.Log(Distance + " " + "From Normal");
 }
 }
 
 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);
         }
 }
 }

Note: I have two other scripts that are almost exactly the same script, but with different directions the other roads are going in.

helpme1.png (197.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

145 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Bullets with raycast and physics 2 Answers

Limit an amount of prefabs instantiated in javascript for Unity 5. 0 Answers

Why wont my script instantiate multiple instances? 1 Answer

ScreenPointToRay 0 Answers

Unity3d 4.6 javascript prefab with script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges