Ejemplo n.º 1
0
    void OnTriggerEnter(Collider other)
    {
        if (((1 << other.gameObject.layer) & mask) != 0) //matched one!
        {
            switch (other.gameObject.layer)
            {
            case 17:
            {         //Signage
                Bounds bounds = new Bounds();
                if (other.transform.parent.parent.name.Equals("StreetSigns"))
                {
                    break;
                }

                BoxCollider boxCol = other as BoxCollider;
                bounds.center = boxCol.center;
                bounds.size   = boxCol.size * 1.25f;
                GameObject boundingCube = CreateBoundingCube(other.transform, other.transform.position + bounds.center, bounds.size);
                GameObject infoTagSign  = CreateInfoTagSign(other.transform.position + bounds.center);
                UpdateIDsAndGos(boundingCube, infoTagSign, other, bounds);
            }
            break;

            case 12:
            {          //obstacle
                Bounds     bounds       = new Bounds();
                GameObject boundingCube = null;
                GameObject infoTag      = null;
                if (other.transform.root.CompareTag("TrafficCar"))
                {
                    if (other.transform.name.Equals("Body1"))
                    {
                        bounds = ComputeBounds(other.transform.root);
                        try
                        {
                            boundingCube = other.transform.Find("Cube").gameObject;
                        }
                        catch (NullReferenceException)
                        {
                            Debug.Log("object incriminated is: ");
                        }

                        infoTag = CreateInfoTag(other.transform.position + bounds.center);
                        UpdateIDsAndGos(boundingCube, infoTag, other, bounds);
                        other.transform.root.gameObject.AddComponent <TrafficCarNavigationLineUrban>();
                    }
                }
                else
                {
                    bounds = ComputeBounds(other.transform);
                    try
                    {
                        boundingCube = other.transform.Find("Cube").gameObject;
                    }
                    catch (NullReferenceException)
                    {
                        Debug.Log("object incriminated is: ");
                    }

                    infoTag = CreateInfoTag(other.transform.position + bounds.center);
                    UpdateIDsAndGos(boundingCube, infoTag, other, bounds);
                }
            }
            break;

            case 8:
            {
                if (other.transform.name.Equals("Body1") && (other.transform.root.CompareTag("TrafficCar") || other.transform.root.CompareTag("TrafficScooter")))
                {
                    Bounds     bounds       = ComputeBounds(other.transform.root);
                    GameObject boundingCube = other.transform.Find("Cube").gameObject;
                    GameObject infoTag      = null;
                    infoTag = CreateInfoTag(other.transform.position + bounds.center);
                    UpdateIDsAndGos(boundingCube, infoTag, other, bounds);
                    TrafficCarNavigationLineUrban trafficCarNavigationLineUrban = other.transform.root.gameObject.AddComponent <TrafficCarNavigationLineUrban>();
                    trafficCarNavigationLineUrban.enabled = false;
                }
            }
            break;

            case 18:     //Roads
            {
                if (other.transform.name.StartsWith("TrafficLight"))
                {
                    CubesAndTags cubesAndTags = new CubesAndTags();

                    foreach (Transform t in other.transform)
                    {
                        if (t.name.StartsWith("Light") && t.gameObject.activeInHierarchy)         //there are some trafficLights not active so I have to exclude them
                        {
                            Bounds     bounds       = ComputeBounds(t);
                            GameObject boundingCube = CreateBoundingCube(t, bounds.center, bounds.size);
                            boundingCube.transform.SetParent(t.GetComponentInChildren <MeshFilter>().gameObject.transform);
                            boundingCube.transform.localPosition = Vector3.zero;
                            boundingCube.transform.localScale   *= 1.25f;
                            GameObject infoTag = CreateInfoTag(bounds.center);
                            boundingCube.GetComponent <Renderer>().enabled = false;
                            infoTag.GetComponent <Canvas>().enabled        = false;
                            cubesAndTags.other = other;         //DARIO
                            cubesAndTags.AddElements(boundingCube, infoTag, bounds);
                        }
                    }

                    Bounds      boundsPost = new Bounds();
                    BoxCollider boxCol     = other as BoxCollider;
                    boundsPost.center = Quaternion.Euler(other.transform.localEulerAngles) * new Vector3(boxCol.center.x * other.transform.localScale.x, boxCol.center.y * other.transform.localScale.y, boxCol.center.z * other.transform.localScale.z);
                    boundsPost.size   = new Vector3(boxCol.size.x * other.transform.localScale.x * 1.25f, boxCol.size.y * other.transform.localScale.y * 1.25f, boxCol.size.z * other.transform.localScale.z);
                    GameObject boundingCubePost = CreateBoundingCube(other.transform, other.transform.position + boundsPost.center, boundsPost.size);
                    GameObject infoTagPost      = null;
                    infoTagPost = CreateInfoTag(other.transform.position + boundsPost.center);
                    boundingCubePost.GetComponent <Renderer>().enabled = false;
                    infoTagPost.GetComponent <Canvas>().enabled        = false;
                    cubesAndTags.AddElements(boundingCubePost, infoTagPost, boundsPost);
                    if (cubesAndTags.boundingCube.Count != 0)         //maybe this is no more requested since at least the Post is added, the trafficLight depends
                    {
                        IDsAndGos.Add(other.gameObject.GetInstanceID(), cubesAndTags);
                    }
                }
            }
            break;
            }
        }
    }
Ejemplo n.º 2
0
    } //this is for dynamic objects

    public void BoundingCubeLerperObstacleDefSF(CubesAndTags cubesAndTags, Bounds bounds, float obstacleSpeed, float acceleration, Sprite sprite, Vector3 trasl, int i)
    {
        Vector3  targetPoint = new Vector3(cubesAndTags.other.transform.position.x, rayCastPos.transform.position.y, cubesAndTags.other.transform.position.z);
        float    dstToTarget = Vector3.Distance(rayCastPos.position, targetPoint);
        Renderer cubeRend    = cubesAndTags.boundingCube[i].GetComponent <Renderer>();
        Canvas   canvas      = cubesAndTags.infoTag[i].GetComponent <Canvas>();

        UpdateInfoTag(cubesAndTags, bounds, Mathf.RoundToInt(obstacleSpeed * 3.6f).ToString(), sprite, dstToTarget, trasl, i);

        Animator    anim  = cubesAndTags.infoTag[i].GetComponent <Animator>();
        AudioSource audio = cubesAndTags.boundingCube[i].GetComponent <AudioSource>();

        Vector3 targetDst = targetPoint + cubesAndTags.other.transform.TransformDirection(0, 0, Vector3.forward.z * 50f);

        Debug.DrawLine(targetPoint, targetDst, Color.magenta);

        Vector3 myDst = rayCastPos.position + rayCastPos.transform.TransformDirection(0, 0, Vector3.forward.z * 150f);

        Vector2 intersection = Vector2.zero;

        if (LineSegmentsIntersection(new Vector2(targetPoint.x, targetPoint.z), new Vector2(targetDst.x, targetDst.z), new Vector2(rayCastPos.position.x, rayCastPos.position.z), new Vector2(myDst.x, myDst.z), out intersection) && (obstacleSpeed >= 0.1f && rigidbody.velocity.magnitude >= 0.1f) /*&& Mathf.Abs(vehicleController.steerInput) <= 0.4f*/)
        {
            Vector3 intersection3D = new Vector3(intersection.x, rayCastPos.position.y, intersection.y);
            Debug.DrawLine(rayCastPos.position, intersection3D, Color.black);
            float distToWarn = rigidbody.velocity.magnitude * ResourceHandler.instance.visualisationVars.freeRunningTime + 0.5f * (Mathf.Pow(rigidbody.velocity.magnitude, 2) / ResourceHandler.instance.visualisationVars.systemAccSF); //DARIO

            if (dstToTarget <= distToWarn)
            {
                float   distToWarnEncoded  = Mathf.Pow(distToWarn, 2.5f);
                float   dstToTargetEncoded = Mathf.Pow(dstToTarget, 2.5f);
                bool    blink    = false;
                Color32 topColor = linesUtils.ChangeMatByDistance(dstToTargetEncoded / Mathf.Abs(distToWarnEncoded), ref blink, ref cubesAndTags);
                topColor.a = 0;
                Color32 bottomColor = linesUtils.ChangeMatByDistance(dstToTargetEncoded / Mathf.Abs(distToWarnEncoded), ref blink, ref cubesAndTags);
                bottomColor.a = 0x51;
                cubeRend.material.SetColor("_Color1", topColor);
                cubeRend.material.SetColor("_Color2", bottomColor);
                cubeRend.enabled = true;
                canvas.enabled   = true;
                UpdateInfoTag(cubesAndTags, bounds, Mathf.RoundToInt(obstacleSpeed * 3.6f).ToString(), sprite, dstToTarget, trasl, i);

                anim.SetFloat("Multiplier", 3.0f);
                anim.SetBool("BlinkLoop", blink);

                PlayAudio(audio, dstToTargetEncoded / distToWarnEncoded, cubesAndTags);

                cubesAndTags.prevState = dstToTargetEncoded / distToWarnEncoded;

                cubesAndTags.dangerState = CubesAndTags.DangerState.YES;

                TrafficCarNavigationLineUrban trafficCarNavigationLineUrban = cubesAndTags.other.transform.root.GetComponent <TrafficCarNavigationLineUrban>();

                if (trafficCarNavigationLineUrban)
                {
                    if (trafficCarNavigationLineUrban.enabled == false)
                    {
                        trafficCarNavigationLineUrban.enabled = true;
                    }

                    Transform lineRenderer = cubesAndTags.other.transform.root.Find("TrafLineRenderer");
                    if (lineRenderer)
                    {
                        lineRenderer.GetComponent <LineRenderer>().enabled = true;
                    }
                }
            }
            else
            {
                bool  blink = false;
                float value = 0f;
                if (cubesAndTags.dangerState == CubesAndTags.DangerState.YES)
                {
                    value = Mathf.Pow(cubesAndTags.prevState, 0.7f);
                    cubesAndTags.prevState = value;
                    if (value >= 0.99f)
                    {
                        cubesAndTags.alreadyEvaluated = true;
                        cubesAndTags.dangerState      = CubesAndTags.DangerState.NONE;
                        if (cubesAndTags.alreadyEvaluated)
                        {
                            envSensingUrban.StartCoroutine(WaitAndDisable(canvas, cubeRend, cubesAndTags));
                        }
                        if (cubesAndTags.other.transform.root.GetComponent <TrafficCarNavigationLineUrban>())
                        {
                            Transform lineRenderer = cubesAndTags.other.transform.root.Find("TrafLineRenderer");
                            if (lineRenderer)
                            {
                                lineRenderer.GetComponent <LineRenderer>().enabled = false;
                            }
                        }
                    }
                }
                else
                {
                    value = 1;
                }

                Color32 topColor    = linesUtils.ChangeMatByDistance(value, ref blink, ref cubesAndTags);
                Color32 bottomColor = linesUtils.ChangeMatByDistance(value, ref blink, ref cubesAndTags);

                topColor.a    = 0;
                bottomColor.a = 0x51;
                cubeRend.material.SetColor("_Color1", topColor);
                cubeRend.material.SetColor("_Color2", bottomColor);

                anim.SetBool("BlinkLoop", false);
            }
        }
        else
        {
            bool  blink = false;
            float value = 0f;
            if (cubesAndTags.dangerState == CubesAndTags.DangerState.YES)
            {
                value = Mathf.Pow(cubesAndTags.prevState, 0.7f);
                cubesAndTags.prevState = value;
                if (value >= 0.99f)
                {
                    cubesAndTags.alreadyEvaluated = true;
                    cubesAndTags.dangerState      = CubesAndTags.DangerState.NONE;
                    if (cubesAndTags.alreadyEvaluated)
                    {
                        envSensingUrban.StartCoroutine(WaitAndDisable(canvas, cubeRend, cubesAndTags));
                    }
                    if (cubesAndTags.other.transform.root.GetComponent <TrafficCarNavigationLineUrban>())
                    {
                        Transform lineRenderer = cubesAndTags.other.transform.root.Find("TrafLineRenderer");
                        if (lineRenderer)
                        {
                            lineRenderer.GetComponent <LineRenderer>().enabled = false;
                        }
                    }
                }
            }
            else
            {
                value = 1;
            }

            Color32 topColor    = linesUtils.ChangeMatByDistance(value, ref blink, ref cubesAndTags);
            Color32 bottomColor = linesUtils.ChangeMatByDistance(value, ref blink, ref cubesAndTags);

            topColor.a    = 0;
            bottomColor.a = 0x51;
            cubeRend.material.SetColor("_Color1", topColor);
            cubeRend.material.SetColor("_Color2", bottomColor);

            anim.SetBool("BlinkLoop", false);
        }
    }//this is for dynamic objects