I have a problem with a very simple script I have. I am making a parody of minecraft that has a different way of placing cubes. I just don’t know how to get rid of the cubes with right click when the spawn point is touching the cube. Please help.
(obviously edited from a shooting script)
var bulletprefab:Transform;
function Update () {
if(Input.GetMouseButtonDown(0))
{
var Bullet = Instantiate(bulletprefab, GameObject.Find("Spawn point").transform.position, Quaternion.identity);
}
}
@script RequireComponent(CharacterController)
2 Answers
2
You question is a confusing when you say “when the spawn point is touching the cube.” I’m going to assume because you are using a right mouse click that you want to destroy the the object under the mouse. Assume you named all the objects “Mine.” Here is a bit of code (Untested) to do that job:
if (Input.GetMouseButtonDown(1)) {
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)) {
if (hit.collider.name == "Mine")
Destroy(hit.collider.gameObject);
}
}
It uses raycasting. There are a bizillion questions about raycasting on this list that you can search out and read. Reference: Physics.Raycast().
You question is a confusing when you say “when the spawn point is touching the cube.” I’m going to assume because you are using a right mouse click that you want to destroy the the object under the mouse. Assume you named all the objects “Mine.” Here is a bit of code (Untested) to do that job:
if (Input.GetMouseButtonDown(1)) {
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)) { if (hit.collider.name == "Mine")
Destroy(hit.collider.gameObject);
}
}
It uses raycasting. There are a bizillion questions about raycasting on this list that you can search out and read. Reference: Physics.Raycast().
I see that you are creating an object. where are you trying to destroy it?
– dorpeleg