- Home /
Unity 3D C# - Gameobject Minecraft Like Placing
This is My code
using UnityEngine;
using System.Collections;
public class KillerBoxSelection : MonoBehaviour {
//locatable objects
public Vector3 spawnLocation;
public GameObject myCube;
public GameObject myCubeLook;
//int Damage = 50;
float Distance;
float MaxDistance = 15;
Transform TheSystem;
void Update (){
//display the diestance and direction of where the player is looking
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * MaxDistance, Color.green);
RaycastHit hitLook;
//Distance = hitLook.distance;
if (Physics.Raycast (transform.position, ray.direction * MaxDistance, out hitLook)) {
if (Distance < MaxDistance)
{
if(hitLook.transform.gameObject.tag == "AllowedSelection")
{
spawnLocation = new Vector3(hitLook.transform.position.x , hitLook.transform.position.y, hitLook.transform.position.z);
Instantiate(myCubeLook, spawnLocation, Quaternion.identity);
//myCubeLook.transform.position = hitLook.transform.position;
}
else
{
Destroy(GameObject.FindWithTag("Killer"));
}
}
}
//left click
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
if(Physics.Raycast (transform.position, ray.direction * MaxDistance, out hit))
{
//spawn the cube at the location selected example a cube object
if(hitLook.transform.gameObject.tag == "AllowedSelection")
{
spawnLocation = new Vector3(hit.transform.position.x , hit.transform.position.y, hit.transform.position.z);
Instantiate(myCube, spawnLocation, Quaternion.identity);
}
}
}
}
}
bascily what im trying to make happend is, that I look at a cube and another cube spawns and when I look away It despawns, right now its just keeps creating its self,I was wondering if anyone can please help me?
What do you mean by despawn? As in, to Destroy it? If so - why?
What exactly do you mean, "When I look away it despawns" ... not the destroy part, just the general idea that you're trying to achieve as it isn't revealed explicit in the code that you've posted ...
You need to elaborate further, I simply can't make any sense of what you're trying to achieve/what seems to be the problem. :)
this part
//Distance = hitLook.distance;
if (Physics.Raycast (transform.position, ray.direction * $$anonymous$$axDistance, out hitLook)) { if (Distance < $$anonymous$$axDistance) { if(hitLook.transform.gameObject.tag == "AllowedSelection") { spawnLocation = new Vector3(hitLook.transform.position.x , hitLook.transform.position.y, hitLook.transform.position.z); Instantiate(myCubeLook, spawnLocation, Quaternion.identity);
//myCubeLook.transform.position = hitLook.transform.position;
}
else
{
Destroy(GameObject.FindWithTag("$$anonymous$$iller"));
}
}
}
when I look at a place where I can place a block it places a slightly transpartent version of the object i am placing. the problem is, is that the block keeps on placing its self while I am looking at the place where I can place the cube.
what I want to happen is that, when I look at the place I want To place the cube, it creates the cube onceand places it in the placing spot, but when im not looking in the placing spot I want it to delete ites self
You need to elaborate a little further... are you only trying to spawn one cube, or is it like a large array of cubes? Where should the cubes spawn? following a grid?
It seems very much to me that you're not sure what you're doing, is this right? ;)
snarf
Your answer
Follow this Question
Related Questions
Need help with building system 1 Answer
How to store player model and then swap the model to an object I Raycast hit? 0 Answers
Creating a teleportation gun 1 Answer
Raycast object tag check? 3 Answers
Rotate Raycast in 3D Space 1 Answer