I am trying to build a Tetris 3D game in Unity but I am stuck at getting the different blocks to interact (to detect each other, to sit one on top of the other rather than going through each other). I have set the ground with a box collider and the spwaned blocks come in with rigidbodies and trigger colliders.
This works for them to nicely settle on the ground but when I try to sit a block on top of another block they both have kinematic rigidbodies and the collision detection doesn't work and they end up overlapping. The problem is if I remove the rigidbody of the already settled block then it will go through the ground.
Any idea how to get 2 spawned objects to detect each other?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class moveBlock : MonoBehaviour
{
public static float speed = 2;
public GameObject[] blocks;
public static List<GameObject> blocksSpawned = new List<GameObject>();
public static List<Rigidbody> blocksRBSpawned = new List<Rigidbody>();
public static bool triggerPlatform = true;
Rigidbody rb;
void Update()
{
Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
Vector3 direction = input.normalized;
Vector3 velocity = direction * speed;
Vector3 moveAmount = velocity * Time.deltaTime;
if (triggerPlatform)
{
blocksSpawned.Add((GameObject)Instantiate(blocks[Random.Range(0, 7)], new Vector3(0, 6, 0), Quaternion.Euler(Vector3.right)));
rb = blocksSpawned[blocksSpawned.Count - 1].GetComponent<Rigidbody>();
blocksRBSpawned.Add(rb);
triggerPlatform = false;
}
/*if (!triggerPlatform)
{
Destroy(blocksRBSpawned[blocksRBSpawned.Count - 1]);
}*/
if (blocksSpawned[blocksSpawned.Count - 1] != null)
{
blocksSpawned[blocksSpawned.Count - 1].transform.Translate(Vector3.down * Time.deltaTime*speed, Space.World);
blocksSpawned[blocksSpawned.Count - 1].transform.Translate(moveAmount);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class trigger : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
moveBlock.triggerPlatform = true;
}
}