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 mikey0002 · May 17, 2016 at 06:06 AM · scripting problemcollisionraycast

Raycast not detected on instantiated object

Hey guys, so I am spawning blocks and basing the spawn location off of the space that has not yet been filled. I have two issues, I am able to create one row of blocks but the raycast goes through them and does not fill the row correctly. I also notice that there is a delay when I play before the blocks start spawning. I know that this may not be the most effective way to code this but it works. Please let me know if you have a solution to either of the problems. Thanks!

 #pragma strict
 
 static var BlockSpawn_1Dist : int;
 static var BlockSpawn_2Dist : int;
 static var BlockSpawn_3Dist : int;
 static var BlockSpawn_4Dist : int;
 static var BlockSpawn_5Dist : int;
 static var BlockSpawn_6Dist : int;
 static var BlockSpawn_7Dist : int;
 static var BlockSpawn_8Dist : int;
 static var BlockSpawn_9Dist : int;
 static var BlockSpawn_10Dist : int;
 
 var rayDirection : Vector3;
 var rayDistance : float = 100f;
 var rayOffSet = Vector3(0,0,2);
 
 var hit : RaycastHit;
 var hit2 : RaycastHit;
 var hit3 : RaycastHit;
 var hit4 : RaycastHit;
 var hit5 : RaycastHit;
 var hit6 : RaycastHit;
 var hit7 : RaycastHit;
 var hit8 : RaycastHit;
 var hit9 : RaycastHit;
 var hit10 : RaycastHit;
 
 static var maxValue : float = 0f;
 
 function Update () {
 
     if (Physics.Raycast(transform.position + (rayOffSet * 1), rayDirection, hit, rayDistance)) {
         BlockSpawn_1Dist = hit.distance;
     }
     Debug.DrawRay(transform.position + (rayOffSet * 1), rayDirection * 20, Color.green, 20);
 
     if (Physics.Raycast(transform.position + (rayOffSet * 2), rayDirection, hit2, rayDistance)) {
         BlockSpawn_2Dist = hit2.distance;
     }
     Debug.DrawRay(transform.position + (rayOffSet * 2), rayDirection * 20, Color.green, 20);
 
     if (Physics.Raycast(transform.position + (rayOffSet * 3), rayDirection, hit3, rayDistance)) {
         BlockSpawn_3Dist = hit3.distance;
     }
     Debug.DrawRay(transform.position + (rayOffSet * 3), rayDirection * 20, Color.green, 20);
 
     if (Physics.Raycast(transform.position + (rayOffSet * 4), rayDirection, hit4, rayDistance)) {
         BlockSpawn_4Dist = hit4.distance;
     }
     Debug.DrawRay(transform.position + (rayOffSet * 4), rayDirection * 20, Color.green, 20);
 
     if (Physics.Raycast(transform.position + (rayOffSet * 5), rayDirection, hit5, rayDistance)) {
         BlockSpawn_5Dist = hit5.distance;
     }
     Debug.DrawRay(transform.position + (rayOffSet * 5), rayDirection * 20, Color.green, 20);
 
     if (Physics.Raycast(transform.position + (rayOffSet * 6), rayDirection, hit6, rayDistance)) {
         BlockSpawn_6Dist = hit6.distance;
     }
     Debug.DrawRay(transform.position + (rayOffSet * 6), rayDirection * 20, Color.green, 20);
 
     if (Physics.Raycast(transform.position + (rayOffSet * 7), rayDirection, hit7, rayDistance)) {
         BlockSpawn_7Dist = hit7.distance;
     }
     Debug.DrawRay(transform.position + (rayOffSet * 7), rayDirection * 20, Color.green, 20);
 
     if (Physics.Raycast(transform.position + (rayOffSet * 8), rayDirection, hit8, rayDistance)) {
         BlockSpawn_8Dist = hit8.distance;
     }
     Debug.DrawRay(transform.position + (rayOffSet * 8), rayDirection * 20, Color.green, 20);
 
     if (Physics.Raycast(transform.position + (rayOffSet * 9), rayDirection, hit9, rayDistance)) {
         BlockSpawn_9Dist = hit9.distance;
     }
     Debug.DrawRay(transform.position + (rayOffSet * 9), rayDirection * 20, Color.green, 20);
 
     if (Physics.Raycast(transform.position + (rayOffSet * 10), rayDirection, hit10, rayDistance)) {
         BlockSpawn_10Dist = hit10.distance;
     }
     Debug.DrawRay(transform.position + (rayOffSet * 10), rayDirection * 20, Color.green, 20);
         
     var raycastDist = new int[10];
 
     raycastDist[0] = BlockSpawn_1Dist;
     raycastDist[1] = BlockSpawn_2Dist;
     raycastDist[2] = BlockSpawn_3Dist;
     raycastDist[3] = BlockSpawn_4Dist;
     raycastDist[4] = BlockSpawn_5Dist;
     raycastDist[5] = BlockSpawn_6Dist;
     raycastDist[6] = BlockSpawn_7Dist;
     raycastDist[7] = BlockSpawn_8Dist;
     raycastDist[8] = BlockSpawn_9Dist;
     raycastDist[9] = BlockSpawn_10Dist;
 
     for(var i = 1; i < raycastDist.length; i++) {
         if (raycastDist[i] > maxValue) {
             maxValue = raycastDist[i];
         }
     }
     Debug.Log(maxValue + " Longest Raycast");
 }

Other Script

 #pragma strict
 
 var blockI : GameObject;
 var blockJ : GameObject;
 var blockL : GameObject;
 var blockO : GameObject;
 var blockS : GameObject;
 var blockT : GameObject;
 var blockU : GameObject;
 var blockW : GameObject;
 var blockX : GameObject;
 var blockZ : GameObject;
 
 var spawnPoint_1 : Transform;
 var spawnPoint_2 : Transform;
 var spawnPoint_3 : Transform;
 var spawnPoint_4 : Transform;
 var spawnPoint_5 : Transform;
 var spawnPoint_6 : Transform;
 var spawnPoint_7 : Transform;
 var spawnPoint_8 : Transform;
 var spawnPoint_9 : Transform;
 var spawnPoint_10 : Transform;
 
 var spawnAt_1 = false;
 var spawnAt_2 = false;
 var spawnAt_3 = false;
 var spawnAt_4 = false;
 var spawnAt_5 = false;
 var spawnAt_6 = false;
 var spawnAt_7 = false;
 var spawnAt_8 = false;
 var spawnAt_9 = false;
 var spawnAt_10 = false;
 
 static var spawnRate : float = 2f;
 static var nextSpawn : float = 0f;
 
 var blockSpawn = true;
 
 function Update () {
 
     if (Time.time > nextSpawn) {
 
         if (BlockSpawner.maxValue == BlockSpawner.BlockSpawn_1Dist){
             spawnAt_1 = true;
             Debug.Log("Went though block spawn");
 
         }
         else if (BlockSpawner.maxValue == BlockSpawner.BlockSpawn_2Dist){
             spawnAt_2 = true;
             Debug.Log("Went though block spawn");
 
         }
         else if (BlockSpawner.maxValue == BlockSpawner.BlockSpawn_3Dist){
             spawnAt_3 = true;
             Debug.Log("Went though block spawn");
 
         }
         else if (BlockSpawner.maxValue == BlockSpawner.BlockSpawn_4Dist){
             spawnAt_4 = true;
             Debug.Log("Went though block spawn");
 
         }
         else if (BlockSpawner.maxValue == BlockSpawner.BlockSpawn_5Dist){
             spawnAt_5 = true;
             Debug.Log("Went though block spawn");
 
         }
         else if (BlockSpawner.maxValue == BlockSpawner.BlockSpawn_6Dist){
             spawnAt_6 = true;
             Debug.Log("Went though block spawn");
 
         }
         else if (BlockSpawner.maxValue == BlockSpawner.BlockSpawn_7Dist){
             spawnAt_7 = true;
             Debug.Log("Went though block spawn");
 
         }
         else if (BlockSpawner.maxValue == BlockSpawner.BlockSpawn_8Dist){
             spawnAt_8 = true;
             Debug.Log("Went though block spawn");
 
         }
         else if (BlockSpawner.maxValue == BlockSpawner.BlockSpawn_9Dist){
             spawnAt_9 = true;
             Debug.Log("Went though block spawn");
 
         }
         else if (BlockSpawner.maxValue == BlockSpawner.BlockSpawn_10Dist){
             spawnAt_10 = true;
             Debug.Log("Went though block spawn");
 
         }
 
         Debug.Log(Time.time);
         BlockSpawer();
         blockSpawn = true;
         nextSpawn += spawnRate;    
     }
 }
 
 function BlockSpawer () {
     while (blockSpawn == true) {    
         var piecePicker : int = Random.Range(0, 10);
 
             Debug.Log(piecePicker);
             switch(piecePicker) {
                 
                 case (0):
                     if (spawnAt_1 == true){
                         var blockInstance = Instantiate(blockI, spawnPoint_1.position, Quaternion.identity);
                         Debug.Log("Safe to start spawning");
                         spawnAt_1 = false;
                     }
                     if (spawnAt_2 == true){
                         var blockInstance2 = Instantiate(blockI, spawnPoint_2.position, Quaternion.identity);
                         Debug.Log("Safe to start spawning");
                         spawnAt_2 = false;
                     }
                     if (spawnAt_3 == true){
                         var blockInstance3 = Instantiate(blockI, spawnPoint_3.position, Quaternion.identity);
                         Debug.Log("Safe to start spawning");
                         spawnAt_3 = false;
                     }
                     if (spawnAt_4 == true){
                         var blockInstance4 = Instantiate(blockI, spawnPoint_4.position, Quaternion.identity);
                         Debug.Log("Safe to start spawning");
                         spawnAt_4 = false;
                     }
                     if (spawnAt_5 == true){
                         var blockInstance5 = Instantiate(blockI, spawnPoint_5.position, Quaternion.identity);
                         Debug.Log("Safe to start spawning");
                         spawnAt_5 = false;
                     }
                     if (spawnAt_6 == true){
                         var blockInstance6 = Instantiate(blockI, spawnPoint_6.position, Quaternion.identity);
                         Debug.Log("Safe to start spawning");
                         spawnAt_6 = false;
                     }
                     if (spawnAt_7 == true){
                         var blockInstance7 = Instantiate(blockI, spawnPoint_7.position, Quaternion.identity);
                         Debug.Log("Safe to start spawning");
                         spawnAt_7 = false;
                     }
                     if (spawnAt_8 == true){
                         var blockInstance8 = Instantiate(blockI, spawnPoint_8.position, Quaternion.identity);
                         Debug.Log("Safe to start spawning");
                         spawnAt_8 = false;
                     }
                     if (spawnAt_9 == true){
                         var blockInstance9 = Instantiate(blockI, spawnPoint_9.position, Quaternion.identity);
                         Debug.Log("Safe to start spawning");
                         spawnAt_9 = false;
                     }
                     if (spawnAt_10 == true){
                         var blockInstance10 = Instantiate(blockI, spawnPoint_10.position, Quaternion.identity);
                         Debug.Log("Safe to start spawning");
                         spawnAt_10 = false;
                     }
                     blockSpawn = false;
 
                     break;
 // There are 9 more cases which basically repeat this code with slight changes.
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

83 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

Related Questions

Switch Scene on Collision with a Box 1 Answer

How do you detect a mouse button click on a Game Object? C# 2 Answers

create object on raycast collision that follows raycast. 0 Answers

Change input to collsiion? 0 Answers

Can someone tell me how to restart the scene if my player hits two objects 2 Answers


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