Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Placing Objects Via Raycast Hits in Unity

Sign upSign InSign upSign InChristopher AdamsFollowLevel Up Coding--ListenShareOverview:In this article I'll be going over how you can spawn an Object where the user clicks using Raycast hits.Create a Prefab:In this example I’m going to just spawn cubes. You can use whatever you like but you will need to create a prefab of the object you want to Spawn. You will also need to create a plane object and assign a box collider component. we will also need to create a tag for it.2. Click the plus icon or right click inside the hierarchy and click 3D Object> Plane.2. Now with the plane selected remove the mesh collider and add box collider.3. create a new tag called Ground and add it.Spawn Objects Via Mouse Click:Now let’s create a new C# script and add it to the Main camera.Inside the script we cast a raycast from the mouse position. We grab the hit point and store it in a vector3 Variable That we use later. If the raycast collides with the object tagged Ground, we set a boolean _canSpawnObjecct to true.We check inside the update if the user pressed the left mouse button and the bool is true, when input is detected and the bool is true. We call a method Called SpawnObect() that instantiates an object at the hit point position with the offset Position specified.The Result:Thats it for this article.Thank you for your time and attention.----Level Up CodingJust following my dreams. Here I will document my skills and what I've learned. Hope you enjoy.Christopher AdamsinLevel Up Coding--2Arslan MirzainLevel Up Coding--5Jacob BennettinLevel Up Coding--68Christopher AdamsinLevel Up Coding--1Arslan MirzainLevel Up Coding--Mina PêcheuxinCodeX--Mina PêcheuxinCodeX--1Jared Amlin--Liam England--Scott Sourile--HelpStatusWritersBlogCareersPrivacyTermsAboutText to speechTeams



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Placing Objects Via Raycast Hits in Unity

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×