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 :)