I am making a game like minecraft( minecraft.net ), and ive been trying to make a script that destroys a block that is in range of the camera. How could i do this?
How are your blocks represented? Do they have colliders? Or are you using some other method of interacting with the environment?
My block is the cube primitive with a collider.
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.forward, ref hit)) {
Destroy(hit.transform.gameObject);
}
I do not understand the code. Can you please explain? Thanks.
Which part(s) are you having trouble with?
Well, how would i use this?
That particular code would likely go in the Update() function of a script associated with the same object that the camera is attached to. (If you want the action to be associated with an input event such as a key press, you’d need to add extra logic to support that.)
i would use Vector3.Distance instead of a raycast
Unless you’ve implemented a custom spatial partitioning system, it seems this would require a distance check against every cube in the scene, yes? That’s liable to be prohibitively expensive for any significant number of cubes (even if you use a squared distance test).
Also, a distance check isn’t equivalent to a raycast. Presumably the OP wants the cube that’s ‘in front of’ the camera to be removed, and a distance check won’t provide this information.
There are other options besides raycasting of course, but it’s not clear to me why using Vector.Distance() should be preferred over a raycast. Can you elaborate?
Im sorry but i still dont understand. Heres my code:
function OnTriggerEnter (other : Collider)
{
Destroy(other.gameObject.tag) = "Block";
}
But i get this error:
you need to put the = "Block"; into the Destroy(other.gameObject.tag) so its like this: Destroy(other.gameObject.tag = "Block");
Aside from not compiling, that code doesn’t do anything meaningful. My guess is that you’re looking for this (untested):
function OnTriggerEnter (other : Collider)
{
if (other.gameObject.tag == "Block") {
Destroy(other.gameObject);
}
}
Although that still doesn’t do exactly what you described in your original post.
A Minecraft-like game is actually a pretty complex place to start for a beginning programmer, IMO. If you’re still learning the basics of programming (which it seems you are), I’d recommend starting with something simpler and/or working through some tutorials and references before proceeding further.
ooh nope i getvmore errors:
That doesn’t make any sense either.
Did you put exactly what Jesse said to put?
actually it would be Destroy(other.gameObject.tag == "Block");
That still doesn’t compile (or make sense).
are you using cs? I’m using Javascript (Unityscript) and it complies correctly, and doesnt give me any errors (except for a non-declared variable, but i didn’t do that any way).
I’m using UnityScript. For me, the following script:
function OnTriggerEnter(other : Collider)
{
Destroy(other.gameObject.tag == "Block");
}
Generates the error:
BCE0023: No appropriate version of ‘UnityEngine.Object.Destroy’ for the argument list ‘(boolean)’ was found.
Which is what I would expect for that code.
Can you post your equivalent code? (I don’t usually use UnityScript - maybe it’s a type-checking issue.)
Regaredless of whether or not it compiles though, the question remains, what are you expecting that code to do? Have you actually tried it? If it doesn’t generate a compile-time error for you, I’d be willing to bet it will generate a run-time error.
To cut to the chase, your code submits the value of the expression ‘other.gameObject.tag == “Block”’, which is of a Boolean type, to Destroy(). There is no version of Destroy() that takes a Boolean as an argument, and in any case it doesn’t make any sense to ‘destroy’ a Boolean variable or value, so the question remains, what do you think that code is going to do exactly?