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 skinchlea · Oct 13, 2017 at 12:48 PM · raycastskills

How can i show a preview of a Prefab in the world before actually spawning it?

I am trying to create a skill where a player can spawn a GameObject in the world once a skill bar is filled. Before they spawn it, i want them to be able to see a preview of it and how it would look in the world, similar to the preview below.

I want to be able to show this object where the player is aiming. I'm ray casting from the camera to the centre of the screen and then getting the co-ordinates of the hit.point. I know how to Instantiate prefabs at that location, but don't know how to show a preview of it beforehand.

Any help would be appreciated as i'm new to Unity and these forums.

alt text

200w-d.gif (96.3 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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Yuvii · Oct 13, 2017 at 12:51 PM

Hey,

Actually, the preview is an instantiated object itself. just make it follow the hit.point position, and when the player clicks, instantiate the real prefab and destroy the preview (there's a lot of possibilities, this is just one, but the 'preview' is an object like all the others).

Hope that helped :)

Comment
Add comment · Show 3 · 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
avatar image skinchlea · Oct 13, 2017 at 01:31 PM 0
Share

That helped, thanks!

However, i'm now having problems with the preview object itself. The object spawns at the hit point, but then does a weird thing where it seems to launch itself into the camera. Any ideas on the problem? I have tried to add a rigidbody but still having the same problem.

$$anonymous$$y code is here just in case i'm doing something wrong. I'm finding the object with tag as i don't know how to find a specific instance of a prefab, but i can figure that out later.alt text

     Vector3 rayOrigin = camera.ViewportToWorldPoint (new Vector3(0.5f, 0.5f, 0));
     RaycastHit hit;

     if (Physics.Raycast (rayOrigin, camera.transform.forward, out hit, range))
     {
         //Debug.Log (hit.point);
         if (spawned == 0) {
             Instantiate ($$anonymous$$inionNotSpawned, hit.point, Quaternion.identity);
             spawned = 1;
         }
     }

     if (spawned == 1)
     {
         Debug.Log ("1");
         //GameObject.FindWithTag("$$anonymous$$inionNS").GetComponent<Rigidbody>().$$anonymous$$ovePosition (hit.point);
         GameObject.FindWithTag("$$anonymous$$inionNS").transform.position = hit.point;
     }


200w-d-1.gif (31.5 kB)
avatar image Yuvii skinchlea · Oct 13, 2017 at 01:43 PM 0
Share

It's because the raycast collides with the preview object

Simply disable the preview's collider and it should be okay !

Anyway you still can optimize a bit more and store your instantiated preview in a variable ins$$anonymous$$d of looking for it at every frame

 GameObject $$anonymous$$inionNS = null;  /*(have to be outside the Update() )*/
 
 Vector3 rayOrigin = camera.ViewportToWorldPoint (new Vector3(0.5f, 0.5f, 0));
      RaycastHit hit;
 
 
      if (Physics.Raycast (rayOrigin, camera.transform.forward, out hit, range))
      {
          //Debug.Log (hit.point);
          if (spawned == 0) {
              $$anonymous$$inionNS = Instantiate ($$anonymous$$inionNotSpawned, hit.point, Quaternion.identity);
              spawned = 1;
          }
      }
 
      if (spawned == 1)
      {
          Debug.Log ("1");
          
          //GameObject.FindWithTag("$$anonymous$$inionNS").transform.position = hit.point;
         $$anonymous$$inionNS.transform.position = hit.point;
      }
avatar image skinchlea Yuvii · Oct 13, 2017 at 02:12 PM 0
Share

Thank you! you've helped a lot!

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

98 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

Related Questions

RaycastHit Triangle Index Problem 0 Answers

Why multiply a vector by -2? 1 Answer

How can I make diagonal collisions work with raycasts from a boxcollider? (3D, C#) 0 Answers

rotating hovercar to be at the same angle as the terrain/object below it 1 Answer

Raycast on Mesh collider 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