void PlayNearestAudio() { List <CubesAndTags> audioIDsAndGos = IDsAndGos.Values.Where(x => x.boundingCube[0] != null).Where(x => x.boundingCube[0].GetComponent <AudioSource>().isPlaying == true).ToList(); CubesAndTags nearest = null; //nearest object whose AudioSource is playing float nearDist = 9999; foreach (var item in audioIDsAndGos) { float thisDist = (transform.position - item.other.transform.position).sqrMagnitude; //this is squaredMagnitude i.e. magnitude without square root if (thisDist < nearDist) { nearDist = thisDist; nearest = item; } } foreach (var item in audioIDsAndGos) { if (item != nearest) { item.boundingCube[0].GetComponent <AudioSource>().Stop(); } } }
private void AggiungiBoxLinea(GameObject go) { if (oggettoInRimozione != null) { if (oggettoInRimozione == go) { oggettoInRimozione = null; } } Collider other = go.transform.Find("CollidersBody/Body1").GetComponent <Collider>(); if (envSelective.IDsAndGos.ContainsKey(other.gameObject.GetInstanceID())) { CubesAndTags cubesAndTags = envSelective.IDsAndGos[other.gameObject.GetInstanceID()]; cubesAndTags.boundingCube[0].GetComponent <Renderer>().enabled = true; cubesAndTags.infoTag[0].GetComponent <Canvas>().enabled = true; go.GetComponent <TrafficCarNavigationLineUrban>().enabled = true; Transform trafLineRenderer = go.transform.Find("TrafLineRenderer"); if (trafLineRenderer != null) { trafLineRenderer.GetComponent <LineRenderer>().enabled = true; } } }
void UpdateIDsAndGos(GameObject boundingCube, GameObject infoTag, Collider other, Bounds bounds) { boundingCube.GetComponent <Renderer>().enabled = false; infoTag.GetComponent <Canvas>().enabled = false; CubesAndTags cubesAndTags = new CubesAndTags(); cubesAndTags.AddElements(boundingCube, infoTag, bounds); cubesAndTags.other = other; //DARIO IDsAndGos.Add(other.gameObject.GetInstanceID(), cubesAndTags); }
void PlayAudio(AudioSource audio, float normDist, CubesAndTags cubesAndTags) { if (cubesAndTags.alreadyPlayed.Equals(false)) { if ((cubesAndTags.other.gameObject.layer.Equals(LayerMask.NameToLayer("obstacle")) && gameObject.transform.root.GetComponent <TrafAIMotor>().playAudio) || (cubesAndTags.other.gameObject.layer.Equals(LayerMask.NameToLayer("Traffic")) && normDist <= 0.2f)) { audio.PlayOneShot(ResourceHandler.instance.audioClips[9]); Debug.Log("audio played"); cubesAndTags.alreadyPlayed = true; } } }
} //this is for dynamic objects public void UpdateInfoTag(CubesAndTags cubesAndTags, Bounds bounds, string text, Sprite sprite, float dstToTarget, Vector3 trasl, int i) { Transform Panel = cubesAndTags.infoTag[i].transform.GetChild(0); if (cubesAndTags.other.gameObject.layer.Equals(16) && !cubesAndTags.other.CompareTag("Sign")) { cubesAndTags.infoTag[i].transform.position = cubesAndTags.other.transform.position + bounds.center + cubesAndTags.infoTag[i].transform.TransformDirection(0, Vector3.up.y * trasl.y, -Vector3.forward.z * trasl.z); Panel.transform.GetChild(0).GetComponent <TextMeshProUGUI>().text = text + " KPH"; //KPH Panel.transform.GetChild(1).GetComponent <TextMeshProUGUI>().text = Mathf.RoundToInt(dstToTarget).ToString() + " M"; //DIST Panel.transform.GetChild(3).GetComponent <Image>().sprite = sprite; //SPRITE } else if ((cubesAndTags.other.gameObject.layer.Equals(16) && cubesAndTags.other.CompareTag("Sign")) || cubesAndTags.other.gameObject.layer.Equals(17)) { float height = cubesAndTags.infoTag[i].GetComponent <RectTransform>().rect.height *cubesAndTags.infoTag[i].transform.localScale.y; cubesAndTags.infoTag[i].transform.position = cubesAndTags.other.transform.position + bounds.center + /*cubesAndTags.infoTag[i].transform.TransformDirection(-Vector3.forward)*/ cubesAndTags.infoTag[i].transform.TransformDirection(0, Vector3.up.y * height, 0); Panel.transform.GetChild(0).GetComponent <TextMeshProUGUI>().text = Mathf.RoundToInt(dstToTarget).ToString() + " M"; //DIST Panel.transform.GetChild(1).GetComponent <Image>().sprite = sprite; //SPRITE } else if (cubesAndTags.other.gameObject.layer.Equals(8)) { cubesAndTags.infoTag[i].transform.position = cubesAndTags.other.bounds.center + cubesAndTags.infoTag[i].transform.TransformDirection(0, Vector3.up.y * bounds.size.y, 0); //I have considered the Collider bounds which gives me the world position of the center of the object. I have to do this otherwise the Scooterman has its center on the ground which is not good in order to position the infoTag correctly. Panel.transform.GetChild(0).GetComponent <TextMeshProUGUI>().text = text + " KPH"; //KPH Panel.transform.GetChild(1).GetComponent <TextMeshProUGUI>().text = Mathf.RoundToInt(dstToTarget).ToString() + " M"; //DIST Panel.transform.GetChild(3).GetComponent <Image>().sprite = sprite; //SPRITE } else if (cubesAndTags.other.gameObject.layer.Equals(12)) { cubesAndTags.infoTag[i].transform.position = cubesAndTags.other.transform.position + cubesAndTags.infoTag[i].transform.TransformDirection(0, Vector3.up.y * (bounds.size.y + cubesAndTags.infoTag[0].transform.localScale.y * cubesAndTags.infoTag[0].GetComponent <RectTransform>().rect.height * 0.5f), 0); Panel.transform.GetChild(0).GetComponent <TextMeshProUGUI>().text = text + " KPH"; //KPH Panel.transform.GetChild(1).GetComponent <TextMeshProUGUI>().text = Mathf.RoundToInt(dstToTarget).ToString() + " M"; //DIST Panel.transform.GetChild(3).GetComponent <Image>().sprite = sprite; //SPRITE } else if (cubesAndTags.other.gameObject.layer.Equals(18)) { cubesAndTags.infoTag[i].transform.position = cubesAndTags.other.transform.position + bounds.center + cubesAndTags.infoTag[i].transform.TransformDirection(0, Vector3.up.y * trasl.y, -Vector3.forward.z * trasl.z); Panel.transform.GetChild(0).GetComponent <TextMeshProUGUI>().text = text + " KPH"; //KPH Panel.transform.GetChild(1).GetComponent <TextMeshProUGUI>().text = Mathf.RoundToInt(dstToTarget).ToString() + " M"; //DIST Panel.transform.GetChild(3).GetComponent <Image>().sprite = sprite; //SPRITE } float distMax = 150f; float distMin = 5.5f; float scaleMax = (infoTagStartScale.x * distMax) / distMin; float offset = Vector3.Distance(cubesAndTags.infoTag[i].transform.position, driverCam.transform.position); float newScale = infoTagResize.Evaluate(offset / distMax) * scaleMax; cubesAndTags.infoTag[i].transform.localScale = new Vector3(newScale, newScale, newScale); cubesAndTags.infoTag[i].transform.LookAt((2 * cubesAndTags.infoTag[i].transform.position - driverCam.transform.position)); }
IEnumerator WaitAndDisable(Canvas canvas, Renderer cubeRend, CubesAndTags cubesAndTags) { cubesAndTags.alreadyEvaluated = false; yield return(new WaitForSeconds(3f)); canvas.enabled = false; cubeRend.enabled = false; if (cubesAndTags.other.transform.root.GetComponent <TrafficCarNavigationLineUrban>()) { Transform lineRenderer = cubesAndTags.other.transform.root.Find("TrafLineRenderer"); if (lineRenderer) { lineRenderer.GetComponent <LineRenderer>().enabled = false; } } }
private void RimuoviBoxLinea(GameObject go) { Collider other = go.transform.Find("CollidersBody/Body1").GetComponent <Collider>(); if (envSelective.IDsAndGos.ContainsKey(other.gameObject.GetInstanceID())) { CubesAndTags cubesAndTags = envSelective.IDsAndGos[other.gameObject.GetInstanceID()]; cubesAndTags.boundingCube[0].GetComponent <Renderer>().enabled = false; cubesAndTags.infoTag[0].GetComponent <Canvas>().enabled = false; Transform trafLineRenderer = go.transform.Find("TrafLineRenderer"); if (trafLineRenderer != null) { trafLineRenderer.GetComponent <LineRenderer>().enabled = false; } } }
void UpdateInfoTag(CubesAndTags cubesAndTags, Bounds bounds, string text, Sprite sprite, float dstToTarget, Vector3 trasl, int i) { Transform Panel = cubesAndTags.infoTag[i].transform.GetChild(0); if (cubesAndTags.other.gameObject.layer.Equals(16)) { cubesAndTags.infoTag[i].transform.position = cubesAndTags.other.transform.position + bounds.center + cubesAndTags.infoTag[i].transform.TransformDirection(0, Vector3.up.y * trasl.y, -Vector3.forward.z * trasl.z); Panel.transform.GetChild(0).GetComponent <TextMeshProUGUI>().text = text + " KPH"; //KPH Panel.transform.GetChild(1).GetComponent <TextMeshProUGUI>().text = Mathf.RoundToInt(dstToTarget).ToString() + " M"; //DIST Panel.transform.GetChild(3).GetComponent <Image>().sprite = sprite; //sprite } else if (cubesAndTags.other.gameObject.layer.Equals(17)) { cubesAndTags.infoTag[i].transform.position = cubesAndTags.other.transform.position + bounds.center + cubesAndTags.infoTag[i].transform.TransformDirection(-Vector3.forward); Panel.transform.GetChild(0).GetComponent <TextMeshProUGUI>().text = Mathf.RoundToInt(dstToTarget).ToString() + " M"; //DIST Panel.transform.GetChild(1).GetComponent <Image>().sprite = sprite; //sprite } else if (cubesAndTags.other.gameObject.layer.Equals(8)) { cubesAndTags.infoTag[i].transform.position = cubesAndTags.other.transform.position + cubesAndTags.infoTag[i].transform.TransformDirection(0, Vector3.up.y * bounds.size.y, 0); Panel.transform.GetChild(0).GetComponent <TextMeshProUGUI>().text = text + " KPH"; //KPH Panel.transform.GetChild(1).GetComponent <TextMeshProUGUI>().text = Mathf.RoundToInt(dstToTarget).ToString() + " M"; //DIST Panel.transform.GetChild(3).GetComponent <Image>().sprite = sprite; //sprite } else if (cubesAndTags.other.gameObject.layer.Equals(12)) { cubesAndTags.infoTag[i].transform.position = cubesAndTags.other.transform.position + cubesAndTags.infoTag[i].transform.TransformDirection(0, Vector3.up.y * (bounds.size.y + cubesAndTags.infoTag[0].transform.localScale.y * cubesAndTags.infoTag[0].GetComponent <RectTransform>().rect.height * 0.5f), 0); Panel.transform.GetChild(0).GetComponent <TextMeshProUGUI>().text = text + " KPH"; //KPH Panel.transform.GetChild(1).GetComponent <TextMeshProUGUI>().text = Mathf.RoundToInt(dstToTarget).ToString() + " M"; //DIST Panel.transform.GetChild(3).GetComponent <Image>().sprite = sprite; //sprite } float distMax = 150f; float distMin = 5.5f; float scaleMax = (infoTagStartScale.x * distMax) / distMin; float offset = Vector3.Distance(cubesAndTags.infoTag[i].transform.position, driverCam.transform.position); float newScale = infoTagResize.Evaluate(offset / distMax) * scaleMax; cubesAndTags.infoTag[i].transform.localScale = new Vector3(newScale, newScale, newScale); cubesAndTags.infoTag[i].transform.LookAt((2 * cubesAndTags.infoTag[i].transform.position - driverCam.transform.position)); }
float CalculateObstacleSpeed(CubesAndTags cubesAndTags) { //if (Application.isPlaying) //{ var t = Time.time; if (t >= cubesAndTags.obstacleNextTime) { Vector3 obstacleNextPos = cubesAndTags.other.transform.position; cubesAndTags.obstaclePrevSpeed = (obstacleNextPos - cubesAndTags.obstaclePrevPos).magnitude / (t - cubesAndTags.obstaclePrevTime); //speed works in play mode, in editor mode since you stop playing, Time.deltatime gives erratic results cubesAndTags.obstaclePrevPos = obstacleNextPos; cubesAndTags.obstaclePrevTime = t; cubesAndTags.obstacleNextTime = t + 0.5f; return(cubesAndTags.obstaclePrevSpeed); } else { return(cubesAndTags.obstaclePrevSpeed); } //} }
void SetCollisionWarning() { List <CubesAndTags> objectsList = envSensing.IDsAndGos.Values.Where(x => x.dangerState != CubesAndTags.DangerState.NONE).ToList(); if (objectsList.Count != 0) { CubesAndTags nearest = null; //nearest object whose AudioSource is playing float nearDist = 9999; foreach (var item in objectsList) { float thisDist = (transform.position - item.other.transform.position).sqrMagnitude; //this is squaredMagnitude i.e. magnitude without square root if (thisDist < nearDist) { nearDist = thisDist; nearest = item; } } switch (nearest.dangerState) { case CubesAndTags.DangerState.YELLOW: { warnImage.sprite = ResourceHandler.instance.sprites[29]; //yellow warning warnImage.enabled = true; } break; case CubesAndTags.DangerState.RED: { warnImage.sprite = ResourceHandler.instance.sprites[30]; //red warning warnImage.enabled = true; } break; } } else { warnImage.enabled = false; } }
} //this is for static objects. In order to show a smooth vanishing when exiting the gradient state I can do a fading animation and control it by a bool that is set in the gradient if and reset after the animation has been played public void BoundingCubeLerperSF(CubesAndTags cubesAndTags, Bounds bounds, float obstacleSpeed, float acceleration, Sprite sprite, Vector3 trasl, int i) { TrafAIMotor trafAIMotor = gameObject.transform.parent.GetComponent <TrafAIMotor>(); Vector3 targetPoint = new Vector3(cubesAndTags.other.transform.position.x, rayCastPos.transform.position.y, cubesAndTags.other.transform.position.z); Vector3 dirToTarget = (targetPoint - rayCastPos.transform.position); float dstToTarget = Vector3.Distance(rayCastPos.position, targetPoint); Renderer cubeRend = cubesAndTags.boundingCube[i].GetComponent <Renderer>(); Canvas canvas = cubesAndTags.infoTag[i].GetComponent <Canvas>(); Animator anim = cubesAndTags.infoTag[i].GetComponent <Animator>(); AudioSource audio = cubesAndTags.boundingCube[i].GetComponent <AudioSource>(); cubeRend.enabled = true; canvas.enabled = true; UpdateInfoTag(cubesAndTags, bounds, Mathf.RoundToInt(obstacleSpeed * 3.6f).ToString(), sprite, dstToTarget, trasl, i); float viewAngle = 5f; if (dstToTarget <= 8f) { viewAngle = 25f; } if (trafAIMotor.hasNextEntry) //If I am waiting at the intersection { float dist = Vector3.Distance(rayCastPos.position, trafAIMotor.currentEntry.waypoints[trafAIMotor.currentEntry.waypoints.Count - 1]); if (dist <= 6.0f) //and I am located near the line that defines the intersection. This second condition is necessary since hasNextEntry is set earlier, not in correspondence with the line of the crossing. { cubeRend.material.SetColor("_Color1", new Color32(0x00, 0x80, 0xFF, 0x00)); cubeRend.material.SetColor("_Color2", new Color32(0x00, 0x80, 0xFF, 0x51)); anim.SetBool("BlinkLoop", false); cubesAndTags.dangerState = CubesAndTags.DangerState.NONE; return; } } if (Vector3.Angle(rayCastPos.TransformDirection(Vector3.forward), dirToTarget) < viewAngle / 2) //the car is in front of me { if (Physics.Raycast(rayCastPos.position, dirToTarget, dstToTarget, mask)) { float distToWarn = RiskAssessmentFormulaD(rigidbody.velocity.magnitude, obstacleSpeed, 0, dstToTarget, ResourceHandler.instance.visualisationVars.systemAccSF); //velocity component of obstacles is null since they are orthogonal to the playerCar if (dstToTarget <= Mathf.Abs(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); anim.SetFloat("Multiplier", 3.0f); anim.SetBool("BlinkLoop", blink); PlayAudio(audio, dstToTargetEncoded / Mathf.Abs(distToWarnEncoded), cubesAndTags); } else { cubeRend.material.SetColor("_Color1", new Color32(0x00, 0x80, 0xFF, 0x00)); cubeRend.material.SetColor("_Color2", new Color32(0x00, 0x80, 0xFF, 0x51)); anim.SetBool("BlinkLoop", false); cubesAndTags.dangerState = CubesAndTags.DangerState.NONE; } } else { cubeRend.material.SetColor("_Color1", new Color32(0x00, 0x80, 0xFF, 0x00)); cubeRend.material.SetColor("_Color2", new Color32(0x00, 0x80, 0xFF, 0x51)); anim.SetBool("BlinkLoop", false); cubesAndTags.dangerState = CubesAndTags.DangerState.NONE; } } else { cubeRend.material.SetColor("_Color1", new Color32(0x00, 0x80, 0xFF, 0x00)); cubeRend.material.SetColor("_Color2", new Color32(0x00, 0x80, 0xFF, 0x51)); anim.SetBool("BlinkLoop", false); cubesAndTags.dangerState = CubesAndTags.DangerState.NONE; } } //this is for dynamic objects
} //this is for dynamic objects public void BoundingCubeLerperDangerousCarPCH(CubesAndTags cubesAndTags, Bounds bounds, float obstacleSpeed, float distToWarn, float viewAngle, Sprite sprite, Vector3 trasl, int i) { Vector3 targetPoint = cubesAndTags.other.transform.position; Vector3 dirToTarget = (targetPoint - rayCastPos.transform.position); float dstToTarget = Vector3.Distance(rayCastPos.position, targetPoint); Renderer cubeRend = cubesAndTags.boundingCube[i].GetComponent <Renderer>(); Canvas canvas = cubesAndTags.infoTag[i].GetComponent <Canvas>(); cubeRend.enabled = true; canvas.enabled = true; 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>(); if (Vector3.Angle(rayCastPos.TransformDirection(Vector3.forward), dirToTarget) < ResourceHandler.instance.visualisationVars.viewAngle / 2) { 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); anim.SetFloat("Multiplier", 3.0f); anim.SetBool("BlinkLoop", blink); PlayAudio(audio, dstToTargetEncoded / Mathf.Abs(distToWarnEncoded), cubesAndTags); cubesAndTags.prevState = dstToTargetEncoded / Mathf.Abs(distToWarnEncoded); //cubesAndTags.gradient = CubesAndTags.Gradient.ON; } // else // { // bool blink = false; // float value = 0f; // if (cubesAndTags.gradient == CubesAndTags.Gradient.ON) // { // value = Mathf.Pow(cubesAndTags.prevState, 0.7f); // cubesAndTags.prevState = value; // } // else // value = 1; // //Debug.Log("obstacle is: " + cubesAndTags.other.name + value); // 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); // cubesAndTags.dangerState = CubesAndTags.DangerState.NONE; // if (value == 1) // cubesAndTags.gradient = CubesAndTags.Gradient.OFF; // } //} //else //{ // bool blink = false; // float value = 0f; // if (cubesAndTags.gradient == CubesAndTags.Gradient.ON) // { // value = Mathf.Pow(cubesAndTags.prevState, 0.7f); // cubesAndTags.prevState = value; // } // else // value = 1; // //Debug.Log("obstacle is: " + cubesAndTags.other.name + value); // 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); // cubesAndTags.dangerState = CubesAndTags.DangerState.NONE; // if (value == 1) // cubesAndTags.gradient = CubesAndTags.Gradient.OFF; } } //this is for dynamic objects
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; } } }
} //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>(); cubeRend.enabled = true; canvas.enabled = true; 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); anim.SetFloat("Multiplier", 3.0f); anim.SetBool("BlinkLoop", blink); PlayAudio(audio, dstToTargetEncoded / distToWarnEncoded, cubesAndTags); cubesAndTags.prevState = dstToTargetEncoded / distToWarnEncoded; cubesAndTags.dangerState = CubesAndTags.DangerState.YES; } 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.dangerState = CubesAndTags.DangerState.NONE; } } 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.dangerState = CubesAndTags.DangerState.NONE; } } 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
} //this is for dynamic objects (scooter) public void BoundingCubeLerperPCH(CubesAndTags cubesAndTags, Bounds bounds, Sprite sprite, Vector3 trasl, int i) { Renderer cubeRend = cubesAndTags.boundingCube[i].GetComponent <Renderer>(); Canvas canvas = cubesAndTags.infoTag[i].GetComponent <Canvas>(); Vector3 targetPoint = cubesAndTags.other.transform.position; Vector3 dirToTarget = (targetPoint - rayCastPos.transform.position); Animator animTag = cubesAndTags.infoTag[i].GetComponent <Animator>(); Animator animCube = cubesAndTags.boundingCube[i].GetComponent <Animator>(); AudioSource audio = cubesAndTags.boundingCube[i].GetComponent <AudioSource>(); float dstToTarget = Vector3.Distance(rayCastPos.position, targetPoint); float viewAngle = 15f; if (dstToTarget <= 8f) { viewAngle = 25f; } if (Vector3.Angle(rayCastPos.TransformDirection(Vector3.forward), dirToTarget) < viewAngle / 2) { if (Physics.Raycast(rayCastPos.position, dirToTarget, dstToTarget, mask)) { float distToWarn = RiskAssessmentFormulaD(rigidbody.velocity.magnitude, 0, 0, dstToTarget, ResourceHandler.instance.visualisationVars.systemAccStaticPCH); //velocity component of obstacles is null since they are orthogonal to the playerCar if (dstToTarget <= Mathf.Abs(distToWarn) && Mathf.Abs(vehicleController.steerInput) == 0f) { Debug.DrawLine(rayCastPos.position, targetPoint, Color.red); 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; cubesAndTags.alreadyEvaluated = true; animTag.SetFloat("Multiplier", 3.0f); animTag.SetBool("BlinkLoop", blink); PlayAudio(audio, dstToTargetEncoded / Mathf.Abs(distToWarnEncoded), cubesAndTags); UpdateInfoTag(cubesAndTags, bounds, "0", sprite, dstToTarget, trasl, i); //cubesAndTags.prevState = dstToTargetEncoded / Mathf.Abs(distToWarnEncoded); } else { cubeRend.enabled = false; canvas.enabled = false; animTag.SetBool("BlinkLoop", false); } } else { cubeRend.enabled = false; canvas.enabled = false; animTag.SetBool("BlinkLoop", false); } } else { cubeRend.enabled = false; canvas.enabled = false; animTag.SetBool("BlinkLoop", false); } } //this is for static objects. OK!
} //this is for static objects. OK! public void BoundingCubeLerperPCH(CubesAndTags cubesAndTags, Bounds bounds, float obstacleSpeed, float acceleration, Sprite sprite, Vector3 trasl, int i) { Vector3 targetPoint = cubesAndTags.other.transform.position; Vector3 dirToTarget = (targetPoint - rayCastPos.transform.position); float dstToTarget = Vector3.Distance(rayCastPos.position, targetPoint); Renderer cubeRend = cubesAndTags.boundingCube[i].GetComponent <Renderer>(); Canvas canvas = cubesAndTags.infoTag[i].GetComponent <Canvas>(); cubeRend.enabled = true; canvas.enabled = true; 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>(); float viewAngle = 45f; if (dstToTarget <= 20f) { viewAngle = 25f; } if (Vector3.Angle(rayCastPos.TransformDirection(Vector3.forward), dirToTarget) < viewAngle / 2) //the car is in front of me { if (Physics.Raycast(rayCastPos.position, dirToTarget, dstToTarget, mask)) { float distToWarn = RiskAssessmentFormulaD(rigidbody.velocity.magnitude, obstacleSpeed, 0, dstToTarget, ResourceHandler.instance.visualisationVars.systemAccPCH); //velocity component of obstacles is null since they are orthogonal to the playerCar if (dstToTarget <= Mathf.Abs(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; anim.SetFloat("Multiplier", 3.0f); anim.SetBool("BlinkLoop", blink); PlayAudio(audio, dstToTargetEncoded / Mathf.Abs(distToWarnEncoded), cubesAndTags); UpdateInfoTag(cubesAndTags, bounds, "0", sprite, dstToTarget, trasl, i); //cubesAndTags.prevState = dstToTargetEncoded / Mathf.Abs(distToWarnEncoded); } else { cubeRend.material.SetColor("_Color1", new Color32(0x00, 0x80, 0xFF, 0x00)); cubeRend.material.SetColor("_Color2", new Color32(0x00, 0x80, 0xFF, 0x51)); anim.SetBool("BlinkLoop", false); cubesAndTags.dangerState = CubesAndTags.DangerState.NONE; } } else { cubeRend.material.SetColor("_Color1", new Color32(0x00, 0x80, 0xFF, 0x00)); cubeRend.material.SetColor("_Color2", new Color32(0x00, 0x80, 0xFF, 0x51)); anim.SetBool("BlinkLoop", false); cubesAndTags.dangerState = CubesAndTags.DangerState.NONE; } } else { cubeRend.material.SetColor("_Color1", new Color32(0x00, 0x80, 0xFF, 0x00)); cubeRend.material.SetColor("_Color2", new Color32(0x00, 0x80, 0xFF, 0x51)); anim.SetBool("BlinkLoop", false); cubesAndTags.dangerState = CubesAndTags.DangerState.NONE; } } //this is for dynamic objects
} //this is for dynamic objects public void BoundingCubeLerperObstaclePCH(CubesAndTags cubesAndTags, Bounds bounds, float obstacleSpeed, float acceleration, Sprite sprite, Vector3 trasl, int i) { Vector3 targetPoint = cubesAndTags.other.transform.position; Vector3 heading = (targetPoint - rayCastPos.position).normalized; //direction from PlayerCar to Obstacle float dot = Vector3.Dot(rayCastPos.transform.forward, heading); //cosine of angle between heading and direction of PlayerCar float dstToTarget = Vector3.Distance(rayCastPos.position, targetPoint); Renderer cubeRend = cubesAndTags.boundingCube[i].GetComponent <Renderer>(); Canvas canvas = cubesAndTags.infoTag[i].GetComponent <Canvas>(); cubeRend.enabled = true; canvas.enabled = true; 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>(); float distToCollision = dstToTarget * dot; //this is the distance from PlayerCar to the endPoint of the triangle cathetus Debug.DrawLine(rayCastPos.position, targetPoint, Color.blue); Vector3 endPoint = rayCastPos.position + (rayCastPos.transform.forward * distToCollision); Debug.DrawLine(rayCastPos.position, endPoint, Color.blue); Vector3 obstacleHeading = endPoint - targetPoint; //this is to understand if the obstacle has passed the endPoint if (Vector3.Dot(obstacleHeading, cubesAndTags.other.transform.forward) > 0) //endPoint is in front of me { float distToWarn = rigidbody.velocity.magnitude * ResourceHandler.instance.visualisationVars.freeRunningTime + 0.5f * (Mathf.Pow(rigidbody.velocity.magnitude, 2) / ResourceHandler.instance.visualisationVars.systemAccPCH); //DARIO if (dstToTarget <= ResourceHandler.instance.visualisationVars.obstacleDistToWarn) { bool blink = false; Color32 topColor = linesUtils.ChangeMatByDistance(dstToTarget / distToWarn /*ResourceHandler.instance.visualisationVars.obstacleDistToWarn*/, ref blink, ref cubesAndTags); topColor.a = 0; Color32 bottomColor = linesUtils.ChangeMatByDistance(dstToTarget / distToWarn /*ResourceHandler.instance.visualisationVars.obstacleDistToWarn*/, ref blink, ref cubesAndTags); bottomColor.a = 0x51; cubeRend.material.SetColor("_Color1", topColor); cubeRend.material.SetColor("_Color2", bottomColor); anim.SetFloat("Multiplier", 3.0f); anim.SetBool("BlinkLoop", blink); PlayAudio(audio, dstToTarget / distToWarn /*ResourceHandler.instance.visualisationVars.obstacleDistToWarn*/, cubesAndTags); cubesAndTags.prevState = dstToTarget / distToWarn /*ResourceHandler.instance.visualisationVars.obstacleDistToWarn*/; //cubesAndTags.gradient = CubesAndTags.Gradient.ON; } // else // { // bool blink = false; // float value = 0f; // if (cubesAndTags.gradient == CubesAndTags.Gradient.ON) // { // value = Mathf.Pow(cubesAndTags.prevState, 0.7f); // cubesAndTags.prevState = value; // } // else // value = 1; // //Debug.Log("obstacle is: " + cubesAndTags.other.name + value); // 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); // cubesAndTags.dangerState = CubesAndTags.DangerState.NONE; // if (value == 1) // cubesAndTags.gradient = CubesAndTags.Gradient.OFF; // } //} //else //{ // bool blink = false; // float value = 0f; // if (cubesAndTags.gradient == CubesAndTags.Gradient.ON) // { // value = Mathf.Pow(cubesAndTags.prevState, 0.7f); // cubesAndTags.prevState = value; // } // else // value = 1; // //Debug.Log("obstacle is: " + cubesAndTags.other.name + value); // 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); // cubesAndTags.dangerState = CubesAndTags.DangerState.NONE; // if (value == 1) // cubesAndTags.gradient = CubesAndTags.Gradient.OFF; } } //this is for dynamic objects