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 Antao98 · Aug 31, 2018 at 10:54 PM · instantiateprefabpreview

Previewing mesh before instantiating

I'm trying to instantiate an object where a raycast hits and I already got that working but now I want to preview it before placing it. I've seen different posts saying that I have to instantiate the preview mesh as well, but I can't seem to instantiate it only once and make it follow the raycast end point.

  bool IsBuilding;
     [SerializeField]
     private GameObject previewMesh;
     [SerializeField]
     private GameObject actualMesh;
     [SerializeField]
     private Camera camera;
     private Vector3 origin;
     private Vector3 direction;
 
     // Use this for initialization
     void Start () {
         IsBuilding = false;
     }
     
     private void FixedUpdate()
     {
         origin = camera.transform.position;
         direction = camera.transform.forward;
 
         if (Input.GetKeyUp(KeyCode.Mouse2) && IsBuilding == false)
         {
             ActivateBuildMode();
             
         }
         else if (Input.GetKeyUp(KeyCode.Mouse2) && IsBuilding == true)
         {
             DeactivateBuildMode();
         }
 
         if (IsBuilding == true)
         {
             RaycastHit hit;
 
             if (Physics.Raycast(origin, direction, out hit, 150) && hit.collider.tag == "Finish")
             {   
                 Instantiate(previewMesh, hit.point, hit.transform.rotation);   
                 Debug.DrawRay(origin, direction, Color.red);
                 if (Input.GetKeyUp(KeyCode.Mouse0))
                 {
                     Instantiate(actualMesh, hit.point, hit.transform.rotation);
                 }
             }
         }
         
     }
 
     void ActivateBuildMode()
     {
         IsBuilding = true;
         Debug.Log("Build Mode Activated");
     }
 
     void DeactivateBuildMode()
     {
         IsBuilding = false;
         Debug.Log("Build Mode Deactivated");
     }
 }
 

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
Best Answer

Answer by Antao98 · Sep 01, 2018 at 12:29 PM

Solved it. Basically created a new boolean and defined the preview mesh in a game object. When I enter build mode it creates the preview mesh and changes the bool, and then I update the instantiated mesh's position. bool IsBuilding; [SerializeField] private GameObject previewMesh; [SerializeField] private GameObject actualMesh; [SerializeField] private Camera camera; private Vector3 origin; private Vector3 direction; bool isCreated = false; GameObject preview;

     // Use this for initialization
     void Start () {
         IsBuilding = false;
     }
     
     private void FixedUpdate()
     {
         origin = camera.transform.position;
         direction = camera.transform.forward;
 
         if (Input.GetKeyUp(KeyCode.Mouse2) && IsBuilding == false)
         {
             ActivateBuildMode();
             
         }
         else if (Input.GetKeyUp(KeyCode.Mouse2) && IsBuilding == true)
         {
             DeactivateBuildMode();
         }
 
         if (IsBuilding == true)
         {
             RaycastHit hit;
 
             if (Physics.Raycast(origin, direction, out hit, 150) && hit.collider.tag == "Finish")
             {   
                 if(isCreated == false)
                 {
                     preview = Instantiate(previewMesh, hit.point, hit.transform.rotation);
                     isCreated = true;
                 }
                 
                 preview.transform.position = hit.point;
                 Debug.DrawRay(origin, direction, Color.red);
                 if (Input.GetKeyUp(KeyCode.Mouse0))
                 {
                     Instantiate(actualMesh, hit.point, hit.transform.rotation);
                     Destroy(previewMesh);
                 }
             }
         }
         
     }
 
     void ActivateBuildMode()
     {
         IsBuilding = true;
         Debug.Log("Build Mode Activated");
     }
 
     void DeactivateBuildMode()
     {
         IsBuilding = false;
         Debug.Log("Build Mode Deactivated");
     }
 }
 
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

129 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

Related Questions

i would like to create a preview of an instantiation but im haveing a problem with my code 1 Answer

How to make an Instantiated prefab a Child of a gameobject that already exists in the hierarchy 3 Answers

How to randomly instantiate other prefabs parallel? 1 Answer

What can prevent a prefab network instantiation? 0 Answers

my object keeps cloning is self i want it to clone once 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