- Home /
Interactive distributed tile network needs to detect player
Hi all,
I'm working on developing a example of a distributed network that displays emergent behavior for designers to play around with to see its potential in interaction designs.
I currently have set up a generic grid creator that sets up a tile network based on some general settings. After that, the wanted number of players are initiated (now still the regular character controller).
The cubes find their neighbors to set up a distributed network. After that, the tiles need to check whether they are activated by the player (through a collision or trigger) and if not, adjust their state in a gradient according to the states of their neighboring tiles.
I have this all working using a collider block as it has a collider attached. However, the cubes in the grid will need to detect the collision on their own using the character that doesn't have a collider attached.
I'm looking for a way to attach a sort of proximity sensor in these tiles using either a collider of a trigger. However when I use a collider, the character cannot walk of falls through the grid (even when using a plane) and when I use triggers, the system stops working and Unity crashes (probably because there are too many triggers).
Can anyone please help? Any tips are greatly appreciated :).
Here is a preview of what I want and have:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class caCube : MonoBehaviour {
public int minGradient = 0;
public int maxGradient = 4;
public int steps = 4; // should only be the distance, nothing else
public Color minColor;
public Color maxColor;
public float radiN = 1.3f;
// hide everything under this---------------------------------------------
[HideInInspector]
// will determine the color of the object
public int gradient = 0;
[HideInInspector]
public int futureGradient = 0;
[HideInInspector]
public bool collide = false;
// list to keep gameobject neighbors in
List<GameObject> neighborList;
// public int neighborCount = 0;
//------------------------------------------------------------------------
// Use this for initialization
void Start ()
{
// Wait untill the grid is created and all nodes are initialized
GameObject g = GameObject.Find("caGrid");
neighborList = new List<GameObject>();
while (g.GetComponent<CA>().gridCreated != true) {
// do nothing
}
// set the variables
gradient = maxGradient;
futureGradient = maxGradient;
// find the surrounding neighbors
FindNeighbors ();
}
// Update is called once per frame
void Update ()
{
// if collide, set to 0, else check neighboring states
if (collide) {
gradient = minGradient;
collide = false;
}
else {
// find the smallest neighbor
int minNeighborGradient = FindSmallestNeighbor();
// increase the gradient bij one step
futureGradient = minNeighborGradient + ((maxGradient - minGradient) / steps);
// constrain gradient values
if (futureGradient <= maxGradient)
{
gradient = futureGradient;
}
else
{
gradient = maxGradient;
}
}
// update the gradient appaerance
UpdateColor ();
}
// will run this from the grid creator after it is done creating
public void FindNeighbors()
{
Collider[] hitColliders = Physics.OverlapSphere(transform.position, radiN);
//Debug.Log ("I am: " + name + " at position: " + transform.position);
foreach (Collider col in hitColliders)
{
//Debug.Log("I hit: " + col.gameObject.name + " at position: " + col.transform.position);
// if it is a node and not directing towards itself
if (col.name == "caCube(Clone)" && col.transform.position != transform.position)
{
neighborList.Add(col.gameObject);
}
// col.SendMessage("message", value, SendMessageOptions.DontRequireReceiver);
}
/*
foreach (GameObject neighbor in neighborList)
{
neighborCount++;
//Debug.Log("I am neighbor " + neighbor.name + " at position: " + neighbor.transform.position);
Debug.DrawLine(transform.position, neighbor.transform.position, Color.magenta, 100f);
}
Debug.Log (neighborCount);
*/
}
int FindSmallestNeighbor()
{
// TODO this should still be done using messages and not
// check for smallest neighboring value;
int smallestN = maxGradient;
foreach (GameObject neighbor in neighborList)
{
if (neighbor.GetComponent<caCube> ().gradient < smallestN)
smallestN = neighbor.GetComponent<caCube> ().gradient;
}
return smallestN;
}
// OnTriggerStay is way to processing intensive
void OnTriggerStay(Collider other)
{
Debug.Log ("test collision" + other.gameObject.name);
if (other.gameObject.name != name && other.gameObject.name != "Plane") {
Debug.Log (other.gameObject.name);
collide = true;
}
}
// to set a collide bool when a collided when reached the cube
void Collide(bool colBool)
{
collide = colBool;
}
void UpdateColor()
{
Color col = Color.Lerp(minColor, maxColor, (float)(gradient - minGradient)/(maxGradient - minGradient));
if (gradient == maxGradient)
renderer.material.color = Color.gray;
else
renderer.material.color = col;
}
}
and the current collider script
using UnityEngine;
using System.Collections;
public class caCollide : MonoBehaviour {
public GameObject cube;
public float speed = 1f;
bool moving = false;
public Collider coll;
// Use this for initialization
void Start () {
coll = GetComponent<Collider>();
}
// Update is called once per frame
void Update () {
//Debug.Log (moving);
if (Input.GetMouseButtonDown (0))
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (coll.Raycast(ray, out hit, 100.0F))
{
this.moving = true;
}
else{
this.moving = false;
}
}
if (this.moving == true)
{
Vector3 move = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
transform.position += move * speed * Time.deltaTime;
}
}
void OnTriggerStay(Collider other)
{
if (other.gameObject.name == "caCube(Clone)") {
other.SendMessage("Collide", true);
//other.gameObject.GetComponent<caCube> ().collide = true;
}
}
}
Ok, so after two days finding for an answer I come up with one immediately after posting this.
I added literally a proximity sensing script in the caCube script and changed the collide boolean to a detected boolean Like this:
void ProximitySense()
{
detected = false;
if (Vector3.Distance (player.transform.position, transform.position) <= detectionRange) {
detected = true;
}
}
Your answer
Follow this Question
Related Questions
Help in Javascript destroying Objects using character controllers and conditionals 1 Answer
CharacterController Collision causing teleportation? 1 Answer
Character controller, player y changes when pushing on colliders 1 Answer
Box collider with rigid body yet enemies still pass through each other. 1 Answer