I am having the worst time with this. I have.
using UnityEngine;
using System.Collections;
public class ConstructionSpacestation : MonoBehaviour
{
public GameObject Builder;
// placeholder for what you see on the screen.
private Transform actualBlock;
// layer to be used
LayerMask mask = -1;
private void Start(){
// no block, quit
if(!Builder) return;
// setup our block
SetupBlock();
}
private void Update (){
// no block, quit
if(!Builder) return;
// make it dissapear
// If we want it to be there, we need to make sure
// we can hit something with the mouse
gameObject.SetActive(false);
// create the ray and hit collector for the Raycast
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
// debug so that if we dont hit something, we quit
if (Physics.Raycast(transform.position, transform.forward, 100, mask.value))return;
// if we hit soemthing with the mouse, we make it appear.
gameObject.SetActive(true);
actualBlock.transform.position = hit.point + hit.normal * 0.5f;
// snap the current block to position
actualBlock.transform.position = new Vector3(
Mathf.Round(actualBlock.position.x),
Mathf.Round(actualBlock.position.y),
Mathf.Round(actualBlock.position.z)
);
if(Input.GetKeyDown(KeyCode.Mouse0)){
// instantiate the block with a collider.
Instantiate(Builder, actualBlock.position, transform.rotation);
}
// no getkeydown, just get key, so you can use it in a combo
if(Input.GetKeyDown(KeyCode.Mouse1) && Input.GetKey(KeyCode.LeftAlt)){
if(hit.transform.gameObject != null)
if(hit.transform.gameObject != gameObject)
Destroy(hit.transform.gameObject);
}
}
// used to create a semi transparent block for us to use.
private void SetupBlock(){
// instantiate the prefab
GameObject obj = Instantiate(Builder, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
// destroy it's colliders so that it wont affect anything
if(obj.collider) Destroy(obj.collider);
// set the block we are going to use in the script
actualBlock = obj.transform;
// setup a new color and texture to use in creating the material for the new block
Color color = new Color(0.2F, 0.3F, 0.4F, 0.5F);
Texture2D texture = new Texture2D(128, 128);
// setup the base renderer
if(obj.renderer){
obj.renderer.material.mainTexture = texture;
obj.renderer.material = new Material(Shader.Find("Transparent/Diffuse"));
obj.renderer.material.mainTexture = texture;
obj.renderer.material.color = color;
}
// setup any other renderers attached so that it all looks uniform.
}
}
To
//New
using UnityEngine;
using System.Collections;
public class ConstructionSpacestation : MonoBehaviour
{
public GameObject Builder;
// placeholder for what you see on the screen.
private Transform actualBlock;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 2000) )
{
Transform actualBlock = hit.transform;
Debug.Log(actualBlock);
actualBlock.position = hit.point + hit.normal * 1f;
actualBlock.transform.position = new Vector3(
Mathf.Round(actualBlock.position.x),
Mathf.Round(actualBlock.position.y),
Mathf.Round(actualBlock.position.z)
);
GameObject Placement = Instantiate(Builder, actualBlock.position, Quaternion.identity) as GameObject;
}
else
{
Debug.Log("nothing");
}
}
}
}
But when I converted it I could not see the “transpartent” prefab. Also it keeps spiting errors on me. I am pulling my hair out, please help me.