- Home /
Break Block Like Minectaft
Hey! im trying to do a minecraft like game. I have a script for breaking grassBlock, but i cant figureout how to make so it only breaks the grass blocks im looking at. I think i should use raycast but i dont know how to do so it detects tags. And should i have a different breakBlock script for every block or 1 script for all the block? because i want it to take different long time to break some blocks.
This is my BreakGrass script:
public int BreakTimer = 3;
public float Timer;
public float Range;
public Camera cam;
void Update ()
{
if (Input.GetMouseButton(0))
{
Timer += Time.deltaTime;
if(Timer >= BreakTimer)
{
BreakBlock();
Timer = 0;
}
}
if (Input.GetKeyUp(KeyCode.Mouse0))
{
Timer = 0;
}
}
public void BreakBlock()
{
Destroy(gameObject);
}
Answer by Captain_Pineapple · Nov 18, 2018 at 03:37 PM
Hey there,
as @Somebody0 wrote you should follow his advice and read into raycasting in general. As for the rest you should perhaps go on like this:
create a basic "Block" class. This class will hold all basic information what a block can have. For example how long it should take to destroy it.
give that script to all blocks that you create.
When you raycast and hit something then the raycast will return you a gameobject.
Check if your "Block" Component exists on this object.
If yes access it and get the amount of time for destruction that you set on the blocks prefab beforehand.
Everything needed for this is described in the tutorial given by Somebody0 as well as the Scripting API behind this Link
Hope this helps.
1 With block class you mean just a new script right?
2 They ray cast iv'e made wont go from the middle of the screen, i made a cube that sends a raycast but that isnt rotating with the camera. im using the standard asset controller
3 how can i make so the block i looked at gets destroyed and not the others?
Hey there,
yeah with class i meant a new script. This is the easiest way to give properties to an object. For your raycast problem you should probably post the code that you came up with. otherwise i cannot tell you whats wrong.
When your raycast works, it will return you an RaycastHit object. This object includes the gameobject that was hit by your ray. (You can read the scripting part and how to access the object here). When you actually hit the object you want then we can talk about how to continue.
@Captain_Pineapple this is my script:
public int BreakTimer = 3;
public float Timer;
public float Range;
public Camera cam;
void Update ()
{
RaycastHit hit;
Ray ray = new Ray(cam.transform.position, Vector3.forward);
Debug.DrawRay(cam.transform.position, Vector3.forward *Range);
if (Input.Get$$anonymous$$ouseButton(0))
{
if(Physics.Raycast(ray, out hit, Range))
{
if (hit.collider.tag == "breakable")
{
Timer += Time.deltaTime;
if (Timer >= BreakTimer)
{
DestroyBlock();
Timer = 0;
}
}
}
}
if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$ouse0))
{
Timer = 0;
}
}
public void DestroyBlock()
{
Destroy(gameObject);
}
i will move the breaktimer and stuff after i've got this to work. And when i rotate my camera the Raycast isn't!
EDIT: i added this print(hit.collider.name);
but i can not aim because the raycast is not rotating
EDIT2: I fixed the breaking everything stuff, by changeing Destroy(gameObject
) to Destroy(hit.collider.gameObject)
but my raycast is still not rotating; i tried changeing it from camera to just a cube but that isnt working
Answer by Somebody0 · Nov 18, 2018 at 10:29 AM
You should learn something about Raycasting. Its way better. (I can't paste there script because i'm on the phone).
Raycasting Unity Video - https://unity3d.com/learn/tutorials/topics/physics/raycasting
Do you think is should make a script for each block: BreakGrass, BreakDirt etc or make one BreakBlock script for all blocks?
Your answer
Follow this Question
Related Questions
Change Transparency 1 Answer
Physics 2D Raycast is not working 3 Answers
Need help with OnUse() script! 1 Answer
Want to move object slowly to where the mouse clicks? 1 Answer
Layer and LayerMask Variables 0 Answers