- Home /
Object has many cube colliders in different angles and collides with more than one object.
Hi, I have a plane object that instantiates many cubic colliders at run time and these colliders point in different angles and in different lengths, each has box collider and rigidbody (please see the picture) these represent a person vision rays. When these colliders hit another object , the plane can turn away as he can see it, but when hit two objects forming a corner the colliding messages delivered from both objects and the plane stuck in the corner. In addition I used 4 ray cast, 2 for forward and 1 to the right in 30 degree and 1 to the left in -30 degree, then check them with the collider (OnTriggerStay) if they all hit then the plane turns away. It turns but goes through one of the objects of the corner or stuck (stand still). How can I make a decision which way should the plane turn to get rid of the corner and how?
using UnityEngine;
using System.Collections;
using System.Text;
using System.IO;
public class Colliders : MonoBehaviour {
public bool hitt=false;
public float startTime;
public float dist, ang;
public float lengthSee, ratio;
public GameObject fps;
public GameObject cols;
public char go = 'i';
public static RaycastHit hitLeft, hitRight, hitRightAngle, hitLeftAngle;
private Vector3 diff, original;
private string line;
public static float [,] ar = new float[15,15];
public static float [,] sn = new float[15,15];
public int i, j, factor;
public Collider [] colCh;
public Vector3[] serHit = new Vector3[3];
void Awake(){
lengthSee = 7.0f;
ratio = lengthSee / 5;
for (i=0; i<15; i++)
for (j=0; j<15; j++)
sn [i, j] = 0;
StreamReader theReader = File.OpenText ("c:\\work\\dataset999.txt");
for (i=0; i<15; i++) {
line = theReader.ReadLine ();
string[] entries = line.Split (' ');
for (j=0; j<15; j++) {
ar [i, j] = float.Parse (entries [j]);
sn [i, j] = (lengthSee - ar [i, j] * ratio);
}
}
theReader.Close ();
fps = GameObject.Find("First Person Controller");
cols = GameObject.Find ("Colliders");
}
void Start(){
startTime = Time.time;
}
void Update(){
if (Input.GetKey ("i"))
go = 'i';
if(Input.GetKey("o"))
go = 'o';
if (hitLeft.distance == 0 && hitRight.distance == 0)
go = 'i';
else
go = 'o';
if (Time.time - startTime < 50 && hitt==false) {
fps.transform.position += fps.transform.forward * Time.deltaTime * 0.01f;
cols.transform.position += cols.transform.forward * Time.deltaTime * 0.01f;
}
dist = hitLeft.distance;
if (hitRight.distance < dist)
dist = hitRight.distance;
}
void OnTriggerEnter(Collider other){
hitt = true;
}
void OnTriggerStay(Collider other){
hitt = true;
Vector3 dir = (other.transform.position - fps.transform.position).normalized ;
ang = Vector3.Dot (dir, fps.transform.right);
if (ang < 0)
factor = 1;
else
factor = -1;
if (go == 'o' && (hitLeft.distance < 2 || hitRight.distance < 2)) {
cols.transform.parent = fps.transform;
turnAway ();
cols.transform.parent = null;
}
if (hitLeft.distance == 0 && hitRight.distance == 0)
goThrough();
if (go == 'o' || other.name == "wall") {
cols.transform.parent = fps.transform;
turnAway ();
cols.transform.parent = null;
}
if (hitLeft.distance >1 && hitLeftAngle.distance >1 && hitRight.distance >1 && hitRightAngle.distance >1 && hitt) {
cols.transform.parent = fps.transform;
while (hitt){
turnAway ();
}
cols.transform.parent = null;
}
}
void OnTriggerExit(Collider other){
hitt = false;
go = 'i';
}
void turnAway(){
fps.transform.Rotate (Vector3.up * (30 * factor) * Time.deltaTime, Space.World);
}
void goThrough(){
colliderOFF ();
fps.transform.position += fps.transform.forward * Time.deltaTime * 0.01f;
cols.transform.position += cols.transform.forward * Time.deltaTime * 0.01f;
}
void colliderOFF(){
foreach (Collider bc in cols.GetComponentsInChildren<Collider> ())
bc.enabled = false;
}
void colliderON(){
foreach (Collider bc in cols.GetComponentsInChildren<Collider> ())
bc.enabled = true;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612040533im_/https://answers.unity.com/themes/thub/images/avi.jpg)