Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Yahya_1967 · Oct 08, 2015 at 08:52 PM · raycastcollidertrigger

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? alt text

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


forum1.jpg (26.3 kB)
Comment
Add comment
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Enemy line of sight using linecast and colliders 1 Answer

Moving objects using raycasting? 1 Answer

Adding force relative to the distance from an object 1 Answer

Prevent shooting when gun is inside wall 1 Answer

Raycast hits a collider and halts. 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