Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Imara · May 15, 2015 at 02:18 PM · collisioncharacter controller

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:

alt text

 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;
         }
     }
 }
 

example.png (269.6 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Imara · May 15, 2015 at 09:08 AM 0
Share

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;
         }
     }

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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

Detecting Collision Between Player and Enemy 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges