public void SpecificSet(Spline newSpline, float fastSpeed, float normalSpeed, float slowSpeed, float cautiousSpeed) { /* Register to the spline */ this.spline = newSpline; spline.GetComponent <StreetMap>().RegisterUser(gameObject); //spline.CalculateLength(); /* Set initial position */ percentage = 0; Vector3 position = spline.GetPosition(percentage); transform.position = StickToTheGround(position); /* Set initial rotation */ transform.rotation = Quaternion.LookRotation(spline.GetDirection(Mathf.Clamp(percentage, 0.01f, 0.99f), true)); // initial rotation /* Set the speeds according to the zones */ this.fastSpeed = fastSpeed; this.normalSpeed = normalSpeed; this.slowSpeed = slowSpeed; this.cautiousSpeed = cautiousSpeed; speedValues = new Dictionary <SPEEDLIMIT, float> { { SPEEDLIMIT.FAST, this.fastSpeed }, { SPEEDLIMIT.NORMAL, this.normalSpeed }, { SPEEDLIMIT.SLOW, this.slowSpeed }, { SPEEDLIMIT.CAUTIOUS, this.cautiousSpeed } }; /* Set the initial speed */ SPEEDLIMIT lastZoneSpeedLimit = SPEEDLIMIT.NORMAL; float lastZonePercentage = 0; speed = 0; foreach (KeyValuePair <float, SPEEDLIMIT> nextSpeedZone in spline.GetComponent <StreetMap>().SpeedZones) { if (percentage >= lastZonePercentage && percentage < nextSpeedZone.Key) { speed = speedValues[lastZoneSpeedLimit]; break; } lastZonePercentage = nextSpeedZone.Key; lastZoneSpeedLimit = nextSpeedZone.Value; } hasStopped = false; movingState = STATE.NORMAL; }
public void UserSet() { // Register to the spline spline.GetComponent <StreetMap>().RegisterUser(gameObject); spline.CalculateLength(); // Set the initial position percentage = startPercentage; Vector3 position = spline.GetPosition(percentage); transform.position = StickToTheGround(position); // Set the initial rotation transform.rotation = Quaternion.LookRotation(spline.GetDirection(Mathf.Clamp(startPercentage, 0.01f, 0.99f), true)); // initial rotation // Set the speed according to the zones speedValues = new Dictionary <SPEEDLIMIT, float> { { SPEEDLIMIT.FAST, fastSpeed }, { SPEEDLIMIT.NORMAL, normalSpeed }, { SPEEDLIMIT.SLOW, slowSpeed }, { SPEEDLIMIT.CAUTIOUS, cautiousSpeed } }; // Set the initial speed SPEEDLIMIT lastZoneSpeedLimit = SPEEDLIMIT.NORMAL; float lastZonePercentage = 0; speed = 0; foreach (KeyValuePair <float, SPEEDLIMIT> nextSpeedZone in spline.GetComponent <StreetMap>().SpeedZones) { if (percentage >= lastZonePercentage && percentage < nextSpeedZone.Key) { speed = speedValues[lastZoneSpeedLimit]; break; } lastZonePercentage = nextSpeedZone.Key; lastZoneSpeedLimit = nextSpeedZone.Value; } hasStopped = false; if (onStart) { movingState = STATE.NORMAL; } else { movingState = STATE.OFF; } }
private void Update() { UpdateSound(); if (movingState != STATE.FREEZE && movingState != STATE.OFF) { /* VFX sound */ if (speed > 10 && loudVFX.GetComponent <ParticleSystem>().isStopped) { loudVFX.GetComponent <ParticleSystem>().Play(); } else if (speed <= 10 && loudVFX.GetComponent <ParticleSystem>().isPlaying) { loudVFX.GetComponent <ParticleSystem>().Stop(); } if (movingState == STATE.NORMAL) { /* FIND THE SPEED LIMIT OF THE ZONE */ SPEEDLIMIT speedLimit = SPEEDLIMIT.NORMAL; SPEEDLIMIT lastZoneSpeedLimit = SPEEDLIMIT.NORMAL; float lastZonePercentage = 0; foreach (KeyValuePair <float, SPEEDLIMIT> nextSpeedZone in spline.GetComponent <StreetMap>().SpeedZones) { if (percentage >= lastZonePercentage && percentage < nextSpeedZone.Key) { speedLimit = lastZoneSpeedLimit;//speedZone.Value; //Debug.Log("Speed Zone : [" + lastZonePercentage + "," + nextSpeedZone.Key + "]"); break; } lastZonePercentage = nextSpeedZone.Key; lastZoneSpeedLimit = nextSpeedZone.Value; } /* UPDATE SPEED */ if (speed < speedValues[speedLimit]) { //speed = Mathf.Min(speed + acceleration * Time.deltaTime / (speedValues[speedLimit] - speed), speedValues[speedLimit]); speed = Mathf.Min(speed + acceleration * Time.deltaTime, speedValues[speedLimit]); Debug.Log("SPEED OF " + gameObject.name + " IS " + speed + " AND CURRENT LIMIT IS " + speedValues[speedLimit]); //speed = speedValues[speedLimit]; } else if (speed > speedValues[speedLimit]) { //speed = Mathf.Max(speed - deceleration * Time.deltaTime / (speed - speedValues[speedLimit]), speedValues[speedLimit]); speed = Mathf.Max(speed - deceleration * Time.deltaTime, speedValues[speedLimit]); //speed = speedValues[speedLimit]; } } else if (movingState == STATE.STAYBEHIND) { /* UPDATE SPEED ACCORDING TO THE FRONT VEHICLE'S SPEED AND POSITION */ // TO DO : Use obstaclePercentage to keep moving till it's not reached // Debug.Log(transform.name + "IN STAYBEHIND MODE"); if (speed >= frontSpeed) { //speed = frontSpeed; speed = Mathf.Max(speed - (Mathf.Max(speed, decelerationObstacle) + 10 * (obstaclePercentage - percentage)) * Time.deltaTime, frontSpeed); // deceleration proportionnal to speed // move towards the target /* * if(speed == frontSpeed && obstaclePercentage > percentage) * { * speed += (obstaclePercentage - percentage) * Time.deltaTime; * }*/ } } else if (movingState == STATE.STOP) { if (speed >= 0) { //speed = 0; speed = Mathf.Max(speed - (Mathf.Max(speed, decelerationStop) - 10 * (intersectionPercentage - percentage)) * Time.deltaTime, 0); // deceleration proportionnal to speed // move towards the target /* * if (speed == 0 && intersectionPercentage > percentage) * { * speed = (intersectionPercentage - percentage) * Time.deltaTime; * }*/ } } /* UPDATE PERCENTAGE */ //spline.CalculateLength(); percentage = Mathf.Min(percentage + (speed * Time.deltaTime) / spline.Length, 1); Debug.Log(gameObject.name + "'S SPEED IS : " + speed + " AND TRUE SPEED IS " + speed * Time.deltaTime / spline.Length); Debug.Log(gameObject.name + "'S PERCENTAGE IS : " + percentage); /* UPDATE TRANSFORM */ Vector3 position = spline.GetPosition(percentage); transform.position = StickToTheGround(position); transform.rotation = Quaternion.LookRotation(spline.GetDirection(percentage)); // Debug.Log("PERCENTAGE : " + percentage + "!!!!!!!!!!!!!!!!!!!!!!!!!!"); /* STOP CONDITION */ /* * if (percentage >= stopPercentage && !hasStopped) * { * hasStopped = true; * if (stopDuration > 0) * { * yield return new WaitForSeconds(stopDuration); * } * }*/ /* OFF CONDITION */ if (percentage == 1) { MovingState = STATE.OFF; //percentage = 0; hasStopped = false; /* Unregister to the previous spline */ spline.GetComponent <StreetMap>().UnregisterUser(gameObject); /* Inform the StreetUsersManager that I am available */ AvailableEvent(gameObject); } } }