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 /
  • Help Room /
avatar image
0
Question by SpectralMagic · May 25, 2018 at 08:53 PM · beginnerbuildingclonesblocksinstantiating

Creating a preview block for a building game[help]

I am trying to create a building game, similar to a game called Besiege.

I am having trouble creating a preview block, a partly opaque block that will show where the block is going to be placed once I left click to place it.

Here is my current code, you can try it using a cube prefab that is 0.25m

C# ;

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MouseRayCast : MonoBehaviour
 {
     public new Camera camera; //MAIN CAMERA
     public Transform activeBlock; //CUBE PREFAB
     bool mouseRelease = true;
 
     void Update()
     {
         RaycastHit hit; //declares raycast
         Ray ray = camera.ScreenPointToRay(Input.mousePosition); //the raycast
         if (Physics.Raycast(ray, out hit)) //if raycast hit something
         {
             Transform objectHit = hit.transform; //declaring where the raycast hit
             var newBlockLocation = objectHit.position + hit.normal / 4; //creating new position
             var newBlockRotation = objectHit.rotation; //creating new rotation
             if (mouseRelease == true)
             {
                 if (Input.GetMouseButtonDown(0) == true) //if mouse is clicked
                 {
                     Instantiate(activeBlock, newBlockLocation, newBlockRotation); //creates the block
                     mouseRelease = false;
                 }
             }
         }
         //test if the player releases so they can place a new block if they did
         if (Input.GetMouseButtonUp(0))
         {
             mouseRelease = true;
         }
     }
 }


I want to instantiate the active block, but remove any colliders on it so it doesn't interfere with the raycast, and change the material on it so I can make it see through, but I dont know where to start.

I've tried instantiating a block that places every frame where you hover your mouse, but I can't get the block to destroy itself after every frame, I also can't seem to figure out how to change its Components(like its collider and material).

I thought about just creating each prefab from code when executing the placing code, and then having a different prefab for the preview block, but I don't want to do that. I want to be able to freely change each block without having to go into the code to change it. Although I do not mind changing the components after the prefab has been instantiated, that would be fine for me :)

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

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.

Follow this Question

Answers Answers and Comments

148 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

i m stuck on building the game when i build the game scene it works gud but when i build with both scene menu and game it stucks sometime on unity logo n sometime in menu scene help me 0 Answers

Help! On/off button 1 Answer

Simple Movement Script Broken 1 Answer

How can I change my player control method to use a touch screen ?? Pong-clone 2D 0 Answers

Absolute beginner. What do I need to know? 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