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 /
avatar image
0
Question by JesterVae · Nov 26, 2017 at 05:24 PM · instantiateraycastchild object

Instantiate On Container Child

Greetings, everyone. I'm new to Unity development, and I've been watching lots of tutorials lately. I appreciate the help in advance; answers provided here have helped a lot.

I've put together some rudimentary code--I'm just testing some things right now--but I'm stuck because I know before I even finish that there must be a better way to do what I want to do. Here's what I'm planning:

A hex-based tile board that instantiates an object in the center of whichever hex tile is touched.

I'm currently using the mouse in place of a touch screen, but this is just test code, anyway. I've figured out how to spawn the object in the center of the container I touched (a simple 3D cylinder for testing). But here's where I'm stuck:

I could create a unique name for each of the 19 hex tiles I intend to have in the end, and then just copy/paste this code 19 times for each container. However, I know there has to be a better way, something like grabbing the child GameObject of the Collider hit by the Ray and Instantiating the "sphere" on that specific child, but I can't figure out how to make it work. Any ideas?

In Unity, I've got an EmptyGameObject called "SpawnPoint" as a child of the Cylinder, and that's where I'm aiming to spawn the sphere. Right now I'm cheating by using a Vector3 to put the sphere where I want it, but that's just me practicing.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class GameManager : MonoBehaviour 
 {    
     public GameObject sphere;
     public GameObject cylinder;

 void Update ()
 {
     if (Input.GetButtonDown("Fire1"))
     {
         RaycastHit hitInfo;

         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);

         if (Physics.Raycast (ray, out hitInfo)) 
         {
             Debug.Log ("Raycast hit:" + hitInfo.collider.gameObject.name);

             if (hitInfo.collider.gameObject.name == "Cylinder")
             {
                 Vector3 spawnPoint = new Vector3 (cylinder.transform.position.x, cylinder.transform.position.y, -200.0f);
                 Instantiate (sphere, spawnPoint , transform.rotation);
             }
         }
     }
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sethuraj · Nov 27, 2017 at 10:31 AM

Hi, There are several ways to accomplish this. Here is a method that came into my mind for the requirement.

Create two scripts 'HexTile' which should be assigned to all the 19 tiles and a 'GameManager' for managing the inputs. Dont forget to add a collider to each hex tiles

 //Script to be added to all the hex tiles
 public class HexTiles : MonoBehaviour
 {
     //Function to add a sphere to this hex
    public void AddSphere(GameObject prefab)
     {
         //Instantiate the sphere prefab
         GameObject obj = Instantiate(prefab);
         //Make the sphere child of this hex tile
         obj.transform.SetParent(this.transform);
         //Reset sphere's position to the tile's center
         obj.transform.localPosition = Vector3.zero;        
     }
 }

And here is the modified GameManager script

 public class GameManager : MonoBehaviour
 {
 
     public GameObject sphere;
 
     void Update()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             RaycastHit hitInfo;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
             if (Physics.Raycast(ray, out hitInfo))
             {
                 Debug.Log("Raycast hit:" + hitInfo.collider.gameObject.name);
 
                 //Try to get the HexTiles script from the hit object
                 HexTiles tile = hitInfo.collider.GetComponent<HexTiles>();
 
                 //If its a valid tile
                 if (tile != null)
                     tile.AddSphere(sphere);
             }
         }
     }
 
 }

Hope this helps !

Comment
Add comment · Share
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

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

116 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

Related Questions

how to make a Portal 2-esque laser beam "redirection" system? 0 Answers

Make an instantiated object match the slope of the terrain 1 Answer

Raycast Specific Object/Instantiate Explosion 1 Answer

How do I instantiate a projectile along the path of a raycast? 0 Answers

How can I continue to instantiate an object after deleting 0 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