using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SinglePooling : MonoBehaviour
{
public Material[] SphereMaterials;
public GameObject Ball;
GameObject[] Spheres;
public Transform spawnPos;
Vector3 RandomForce;
void Start()
{
Spheres = new GameObject[SphereMaterials.Length];
for (int i = 0; i < SphereMaterials.Length; i++)
{
Spheres[i] = GameObject.Instantiate(Ball);
Spheres[i].GetComponent<MeshRenderer>().material = SphereMaterials[i];
Spheres[i].SetActive(false);
}
}
public void ClickyClicky()
{
RandomForce = new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-1f, 1f)) * 1000f;
for (int i = 0; i < Spheres.Length; i++)
{
if(Spheres[i].activeInHierarchy == false)
{
Spheres[i].SetActive(true);
Spheres[i].transform.position = spawnPos.position;
StartCoroutine(waitforforce(Spheres[i], RandomForce));
break;
}
}
}
IEnumerator waitforforce(GameObject forcethis, Vector3 Force)
{
yield return new WaitForSeconds(0.1f);
forcethis.GetComponent<BallBounce>().Bounce(Force);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallBounce : MonoBehaviour
{
Decal decalSpawner;
Rigidbody rb;
void Start()
{
decalSpawner = GetComponent<Decal>();
rb = GetComponent<Rigidbody>();
StartCoroutine(waitforforce());
}
IEnumerator waitforforce()
{
Vector3 randomStart = new Vector3(Random.Range(-1f, 1), 0, Random.Range(-1f, 1));
yield return new WaitForSeconds(0.1f);
Bounce(randomStart * 1000f);
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "WallArea")
{
Quaternion Qua = Quaternion.FromToRotation(Vector3.forward, collision.GetContact(0).normal);
Vector3 hitLocation = new Vector3(
collision.GetContact(0).point.x + (collision.GetContact(0).normal.x * 0.01f),
collision.GetContact(0).point.y + (collision.GetContact(0).normal.y * 0.01f),
collision.GetContact(0).point.z + (collision.GetContact(0).normal.z * 0.01f));
decalSpawner.DrawDecal(hitLocation, Qua);
}
}
public void Bounce(Vector3 direction)
{
rb.AddForce(direction);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Decal : MonoBehaviour
{
Material decalMaterial;
Mesh decalMesh;
private Vector3[] newVertices =
{
new Vector3(0,0,0),
new Vector3(1,0,0),
new Vector3(1,1,0),
new Vector3(0,1,0)
};
private int[] newTriangles = { 0, 1, 3, 1,2,3 };
private const int indexsize = 1000;
private int currIndex = 0;
private Matrix4x4[] matrixList = new Matrix4x4[indexsize];
void Start()
{
decalMaterial = GetComponent<MeshRenderer>().material;
decalMesh = new Mesh();
decalMesh.vertices = newVertices;
decalMesh.triangles = newTriangles;
}
void Update()
{
Graphics.DrawMeshInstanced(decalMesh, 0, decalMaterial, matrixList);
}
public void DrawDecal(Vector3 position, Quaternion rotation)
{
matrixList[currIndex].SetTRS(position, rotation, Vector3.one);
currIndex = (currIndex + 1) % matrixList.Length;
}
}