- Home /
Question by
splendex · Sep 30, 2021 at 08:03 PM ·
unity 5gameobjectprogrammingbreak
Trying to break a block and I need to create a different script for each block. Is it possible to have some sort of class that stores the blocks so I can break any block?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Break : MonoBehaviour
{
public BlockInfo blocks;
public Camera cam;
public float range;
public float timer;
RaycastHit hit;
void Update()
{
Ray ray = new Ray(cam.transform.position, cam.transform.forward);
Debug.DrawRay(cam.transform.position, cam.transform.forward * range, Color.red);
if(Input.GetMouseButton(0)){
if(Physics.Raycast(ray, out hit, range))
{
if(hit.collider.tag == "breakable")
{
timer += Time.deltaTime;
if(timer >= blocks.breakTimer)
{
DestroyBlock();
}
}
}
}
if(Input.GetMouseButtonUp(0)){
timer = 0;
}
}
public void DestroyBlock()
{
Destroy(hit.collider.gameObject);
}
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
transform.SetParent(null); Not Working 1 Answer
How to make a Interactive Computer Screen in Unity 5.5? 1 Answer
how to show coordinates on the prefab? Using multidimensional arrays? 0 Answers
How to keep the position of an object after parenting in timeline? 0 Answers