void Update() { if (!inited) { return; } if (!currentEntry.isIntersection() && currentIndex > 0 && !hasNextEntry) { //last waypoint in this entry, grab the next path when we are in range if (Vector3.Distance(nose.transform.position, currentEntry.waypoints[currentEntry.waypoints.Count - 1]) <= giveWayRegisterDistance) { var node = system.roadGraph.GetNode(currentEntry.identifier, currentEntry.subIdentifier); RoadGraphEdge newNode; if (fixedRoute) { if (++currentFixedNode >= fixedPath.Count) { currentFixedNode = 0; } newNode = system.FindJoiningIntersection(node, fixedPath[currentFixedNode]); } else { newNode = node.SelectRandom(); } if (newNode == null) { Debug.Log("no edges on " + currentEntry.identifier + "_" + currentEntry.subIdentifier); Destroy(gameObject); inited = false; return; } nextEntry = system.GetEntry(newNode.id, newNode.subId); nextTarget = nextEntry.waypoints[0]; hasNextEntry = true; nextEntry.RegisterInterest(this); //see if we need to slow down for this intersection intersectionCornerSpeed = Mathf.Clamp(1 - Vector3.Angle(nextEntry.path.start.transform.forward, nextEntry.path.end.transform.forward) / 90f, 0.4f, 1f); } } //check if we have reached the target waypoint if (Vector3.Distance(nose.transform.position, target) <= waypointThreshold)// && !hasStopTarget && !hasGiveWayTarget) { if (++currentIndex >= currentEntry.waypoints.Count) { if (currentEntry.isIntersection()) { currentEntry.DeregisterInterest(); var node = system.roadGraph.GetNode(currentEntry.identifier, currentEntry.subIdentifier); var newNode = node.SelectRandom(); if (newNode == null) { Debug.Log("no edges on " + currentEntry.identifier + "_" + currentEntry.subIdentifier); Destroy(gameObject); inited = false; return; } currentEntry = system.GetEntry(newNode.id, newNode.subId); nextEntry = null; hasNextEntry = false; targetTangent = (currentEntry.waypoints[1] - currentEntry.waypoints[0]).normalized; } else { if (hasStopTarget || hasGiveWayTarget) { target = nextEntry.waypoints[0]; } else { currentEntry = nextEntry; nextEntry = null; hasNextEntry = false; targetTangent = Vector3.zero; } } if (!hasStopTarget && !hasGiveWayTarget) { currentIndex = 0; } } if (currentIndex > 1) { targetTangent = Vector3.zero; } if (!hasStopTarget && !hasGiveWayTarget) { target = currentEntry.waypoints[currentIndex]; } } SteerCar(); if (hasNextEntry && nextEntry.isIntersection() && nextEntry.intersection.stopSign) { if (stopEnd == 0f) { hasStopTarget = true; stopTarget = nextTarget; stopEnd = Time.time + stopLength; } else if (Time.time > stopEnd) { if (nextEntry.intersection.stopQueue.Peek() == this) { hasGiveWayTarget = false; hasStopTarget = false; stopEnd = 0f; } } } if (hasNextEntry && nextEntry.isIntersection() && !nextEntry.intersection.stopSign) { //check next entry for stop needed if (nextEntry.MustGiveWay()) { hasGiveWayTarget = true; stopTarget = target; } else { hasGiveWayTarget = false; } } if (!hasGiveWayTarget && hasNextEntry && nextEntry.light != null) { if (!hasStopTarget && nextEntry.light.State == TrafLightState.RED) { //light is red, stop here hasStopTarget = true; stopTarget = nextTarget; } else if (hasStopTarget && nextEntry.light.State == TrafLightState.GREEN) { //green light, go! hasStopTarget = false; return; } else if (!hasStopTarget && nextEntry.light.State == TrafLightState.YELLOW) { //yellow, stop if we aren't zooming on through //TODO: carry on if we are too fast/close if (Vector3.Distance(nextTarget, nose.transform.position) > yellowLightGoDistance) { hasStopTarget = true; stopTarget = nextTarget; } } } float targetSpeed = maxSpeed; //check in front of us if (Time.time > nextRaycast) { hitInfo = new RaycastHit(); somethingInFront = CheckFrontBlocked(out hitInfo); nextRaycast = NextRaycastTime(); } if (somethingInFront) { float frontSpeed = Mathf.Clamp((hitInfo.distance - 1f) / 3f, 0f, maxSpeed); if (frontSpeed < 0.2f) { frontSpeed = 0f; } targetSpeed = Mathf.Min(targetSpeed, frontSpeed); } if (hasStopTarget || hasGiveWayTarget) { Vector3 targetVec = (stopTarget - nose.transform.position); float stopSpeed = Mathf.Clamp(targetVec.magnitude * (Vector3.Dot(targetVec, nose.transform.forward) > 0 ? 1f : 0f) / 3f, 0f, maxSpeed); if (stopSpeed < 0.24f) { stopSpeed = 0f; } targetSpeed = Mathf.Min(targetSpeed, stopSpeed); } //slow down if we need to turn if (currentEntry.isIntersection() || hasNextEntry) { targetSpeed = targetSpeed * intersectionCornerSpeed; } else { targetSpeed = targetSpeed * Mathf.Clamp(1 - (currentTurn / maxTurn), 0.1f, 1f); } if (targetSpeed > currentSpeed) { currentSpeed += Mathf.Min(maxAccell * Time.deltaTime, targetSpeed - currentSpeed); } else { currentSpeed -= Mathf.Min(maxBrake * Time.deltaTime, currentSpeed - targetSpeed); if (currentSpeed < 0) { currentSpeed = 0; } } }
//ANTONELLO private void modificaTarget(bool waypointSaltato) { if (++currentIndex >= currentEntry.waypoints.Count) { //Debug.Log("Numero waypoints: " + currentEntry.waypoints.Count + "; currentIndex: " + currentIndex); if (currentEntry.isIntersection()) { currentEntry.DeregisterInterest(); var node = system.roadGraph.GetNode(currentEntry.identifier, currentEntry.subIdentifier); var newNode = node.SelectRandom(); //CONTROLLA QUESTO PEZZO DI CODICE; POTREBBE DISTRUGGERE LA MACCHINA if (newNode == null) { //Debug.Log("no edges on " + currentEntry.identifier + "_" + currentEntry.subIdentifier); Destroy(gameObject); inited = false; return; } currentEntry = system.GetEntry(newNode.id, newNode.subId); nextEntry = null; hasNextEntry = false; inizioValutazioneSemaforo = false; if (this.tag.Equals("Player")) { this.Property = "" + newNode.id; } targetTangent = (currentEntry.waypoints[1] - currentEntry.waypoints[0]).normalized; } else { if ((hasStopTarget || hasGiveWayTarget) && !waypointSaltato) //ANTONELLO { targetPrecedente = target; target = nextEntry.waypoints[0]; } else { if (nextEntry != null && nextEntry.identifier != 0) //ANTONELLO { currentEntry = nextEntry; nextEntry = null; hasNextEntry = false; inizioValutazioneSemaforo = false; targetTangent = Vector3.zero; if (this.tag.Equals("Player")) { this.Property = "" + currentEntry.identifier; } } //ANTONELLO if (waypointSaltato && (hasStopTarget || hasGiveWayTarget)) { hasStopTarget = false; hasGiveWayTarget = false; } } } if ((!hasStopTarget && !hasGiveWayTarget) || waypointSaltato) { currentIndex = 0; } } if (currentIndex > 1) { targetTangent = Vector3.zero; } if ((!hasStopTarget && !hasGiveWayTarget) || waypointSaltato) { targetPrecedente = target; target = currentEntry.waypoints[currentIndex]; } }