Hello, I have a problem with my script. When I press down, it drills a block but it doesn’t set CanBreak to true. It just won’t let my drill another block. I know it has something to do with the boolean CanBreak. My debug log says this:
Hi here is the script
It casts a ray downwards and checks the tag
Edit Working
public var CanBreak:boolean = true;
var range:float = 1;
function Update(){
if(Input.GetButton("Down") && CanBreak){
CanBreak = false;
Drill();
}
if(!Input.GetButton("Down")){
CanBreak = true;
}
}
function Drill(){
var hit:RaycastHit;
if(Physics.Raycast(transform.position,-Vector3.up,hit,range)){
if(hit.collider.tag == "Dirt"){
Destroy(hit.collider.gameObject);
CanBreak = true;
}
if(hit.collider.tag == "Stone"){
Destroy(hit.collider.gameObject);
CanBreak = true;
}
if(!hit.collider){
CanBreak = true;
}
}
}
Note hit should be typed(Physics.Raycast(transform.position,-Vector3.up,hit <-- tells what type of ray to be casted , if not typed it wont hit any object at all ,which in turn returns null,range))before range else it will only cast a ray and wont know what type of ray
Are you calling DrillDown() somewhere else also? If not then I guess problem is with canBreak = false in the if condition when you are calling in Update function. You assign canBreak = true in DrillDown() and again make it false when the control returns back to the Update function.
Solution : I guess if you make canBreak = false in the DrillDown() and canBreak = true in the Update() outside the if will do the stuff ok. Try it out.