- Home /
How do I make a block like minecraft where it gets...
...destroyed when I hit it 3 times(this is easy to do) and if I look away from the block it resets the int?
Haven't really played $$anonymous$$inecraft, but I'd take a look at this post that talks about detecting if an object is in the view of a particular camera. http://answers.unity3d.com/questions/8003/how-can-i-know-if-a-gameobject-is-seen-by-a-partic.html
Answer by hu90 · May 31, 2015 at 10:06 PM
In Minecraft, the hit counter is not reset when the object gets offscreen, but rather when the cursor (center of viewport) is no more over the block. The obvious advantage of doing so is to need only one counter instead of one for every single block.
Just cast a ray from the center of the screen in the camera direction, and check when the target changes:
var currentTarget:GameObject = null;
function Update() {
var ray = camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
var hit;
var newTarget:GameObject = null;
if (Physics.Raycast(ray, hit)) {
newTarget = hit.collider.gameObject;
}
if (newTarget != currentTarget)
ResetHitCount();
currentTarget = newTarget;
}
Answer by hexagonius · May 31, 2015 at 09:46 PM
Use the OnBecameInvisible message. It will fire when the object gets off screen. reset your block there
Your answer
![](https://koobas.hobune.stream/wayback/20220613200507im_/https://answers.unity.com/themes/thub/images/avi.jpg)