Exemple #1
0
    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;
        }
    }
Exemple #2
0
        /// <summary>
        /// Calculates the length of the generated path in world units.
        /// </summary>
        /// <param name="from">The start of the segment to calculate the length of</param>
        /// <param name="to">The end of the segment</param>
        /// <returns></returns>
        public virtual float CalculateLength(double from = 0.0, double to = 1.0, EvaluateMode mode = EvaluateMode.Cached)
        {
            if (mode == EvaluateMode.Accurate)
            {
                return(spline.CalculateLength(from, to));
            }
            float   length         = 0f;
            Vector3 pos            = EvaluatePosition(from);
            int     sampleIndex    = DMath.CeilInt(from * (samples.Length - 1));
            int     endSampleIndex = DMath.FloorInt(to * (samples.Length - 1));

            for (int i = sampleIndex; i < endSampleIndex; i++)
            {
                length += Vector3.Distance(samples[i].position, pos);
                pos     = samples[i].position;
            }
            length += Vector3.Distance(EvaluatePosition(to), pos);
            return(length);
        }
Exemple #3
0
        void CreateSpline()
        {
            spline        = new Spline(customPathType, customPathSampleRate);
            spline.points = points;
            if (loop)
            {
                spline.Close();
            }
            pathLength = spline.CalculateLength();
            float travel = pathLength / (spline.iterations - 1);

            samples    = new SplineSample[spline.iterations];
            samples[0] = spline.Evaluate(0.0);
            for (int i = 1; i < spline.iterations - 1; i++)
            {
                samples[i] = spline.Evaluate(spline.Travel(samples[i - 1].percent, travel, Spline.Direction.Forward));
            }
            samples[spline.iterations - 1] = spline.Evaluate(1.0);
        }
Exemple #4
0
    private void Start()
    {
        percentage = startPercentage;
        spline.CalculateLength();

        if (groundLevel != null)
        {
            groundOffset = transform.position.y - groundLevel.transform.position.y;
        }
        movingState = STATE.NORMAL;

        Vector3 position = Vector3.zero;

        if (directionState == DIRECTION.FORWARD)
        {
            position           = spline.GetPosition(percentage);
            transform.rotation = Quaternion.LookRotation(spline.GetDirection(Mathf.Max(percentage, 0.01f), true)); // initial rotation
        }
        else if (directionState == DIRECTION.BACKWARD)
        {
            position           = spline.GetPosition(1 - percentage);
            transform.rotation = Quaternion.LookRotation(Quaternion.Euler(0, 180, 0) * spline.GetDirection(Mathf.Min(1 - percentage, 0.99f), true)); // initial rotation
        }
        if (raycaster != null && groundLevel != null)
        {
            transform.position = StickToTheGround(position);
        }
        else
        {
            transform.position = position;
        }

        if (onStart)
        {
            StartCoroutine("Move");
        }
    }