• 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 /
avatar image
Question by loizos-95b · Dec 02, 2017 at 07:47 PM · gameobjectraycastinstantiateupdatespawn

How should i add raycast to multiple game objects???

Hello coders, I would like to know some possible ways to create the same raycast for multiple game-objects. I want to use raycast to check if two obstacles(walls) are colliding and if yes, change the position of the wall to avoid obstacles collisions. Here is the script that is attached on a empty gameobject (not movable) in the terrain :


 public class Spawn_Objects_Script : MonoBehaviour {
         /* Get Original Prefabs */
         public GameObject Original_wallPrefab;
     
         /* Declare unrecognizable (Missing Objects) */
         public GameObject PlayerPrefabForClones;
     
         /* Declare a number of spawnings for each prefab */
         public static int numberOfWalls = 10;
     
         /* Create a new group for each object */
        public static GameObject[] WallsGroup = new GameObject[numberOfWalls];
         
         /* Create an array of Game Objects to hold the Prefabs */
         public GameObject[] MySpawningPrefabs; 
 
         private Vector3 position;
     
         [HideInInspector]
         public Vector3 RaycastStartPosition;
     
         void Start () {
             StartCoroutine("SpawnWorld");
         }
 
         void Update()
         {
             //Raycast(RaycastStartPosition)
         }
     
         IEnumerator SpawnWorld()
         {
             //Wait for 2 seconds every time you spawn something
             WaitForSeconds wait = new WaitForSeconds(2.5f); 
             
     
             for (int i = 0; i < WallsGroup.Length; i++)
             {
                 //position = AvoidObstaclesCollition();
                 position = new Vector3(Random.Range(300, 600), 155, Random.Range(235, 710));
     
                 WallsGroup[i] = Instantiate(Original_wallPrefab, position, Quaternion.identity) as GameObject;
     
                 RaycastStartPosition = new Vector3(position.x, position.y + 5f, position.z);
     
                 Raycast(RaycastStartPosition);
             }
             yield return wait;
         }
         
         private Vector3 AvoidObstaclesCollition()
         {
             while (true)
             {
                 position = new Vector3(Random.Range(300, 600), 155, Random.Range(235, 710));
     
                 RaycastHit hit;
     
                 if (Physics.Raycast(position, -Vector3.up, out hit, Mathf.Infinity))
                 {
                     Collider[] objectsHit = Physics.OverlapSphere(position,10, 0);
                     //.OverlapBox(position, 10, null);
                     if (objectsHit.Length == 0)
                     {
                         return position;
                     }
                 }
             }
         }
     
     
         void Raycast(Vector3 RaycastStartPosition)
         {
             Vector3 forward = transform.TransformDirection(Vector3.forward) * 100;
             Vector3 back = transform.TransformDirection(Vector3.back) * 100;
             Vector3 left = transform.TransformDirection(Vector3.left) * 100;
             Vector3 right = transform.TransformDirection(Vector3.right) * 100;
     
             Debug.DrawRay(RaycastStartPosition, forward, Color.red);
             Debug.DrawRay(RaycastStartPosition, back, Color.green);
             Debug.DrawRay(RaycastStartPosition, left, Color.yellow);
             Debug.DrawRay(RaycastStartPosition, right, Color.cyan);
     
         }
     }

  



Solution 1: Call the Raycast method in the void Update() ------> Issue: Create a raycast only at the last wall(10th wall) alt text


Solution 2: Call the Raycast method in the IEnumerator inside the for loop ------> Issue: Create the raycast only at the first frame(Visible only when the pause is pressed before the play Mode)

 for (int i = 0; i < WallsGroup.Length; i++){
 
      . . .
 
   RaycastStartPosition = new Vector3(position.x, position.y + 5f, position.z);
 
      Raycast(RaycastStartPosition);
 }

alt text

IT WILL BE GREATLY APPRECIATED IF YOU HAVE ANY SOLUTION ON MIND JUST LET ME KNOW, THANKS FOR YOUR TIME.

update.png (297.4 kB)
forloop.png (251.9 kB)
Comment

People who like this

0 Show 0
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

138 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

Related Questions

Keep random Instantiation from spawning ontop of other objects 1 Answer

[SOLVED] Only instantiating once 1 Answer

The Weirdest Bug Ever: GameObject Transform Appears in Editor to Be in One Spot but Instantiates and Registers in Another 2 Answers

Instantiate multiple objects and have them raycast 1 Answer

New to unity - I am using this script to add prefabs to an inventory array on click. How can I remove those prefabs/gameobjects from my array slots once I have instantiated them? see the example code below: 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