// Update is called once per frame
    void Update()
    {
        Vector3 oldPos               = transform.position;
        var     splineCurve          = splineComponent.GetSplineObject();
        Vector3 currentCurveVelocity = splineCurve.GetVelocity(currentPosition);

        currentPosition += Time.deltaTime * speed / currentCurveVelocity.magnitude;
        if (currentPosition > splineCurve.totalTime)
        {
            currentPosition = 0;
        }
        transform.position = splineCurve.GetPosition(currentPosition);
        currentSpeed       = (transform.position - oldPos).magnitude / Time.deltaTime;
    }
示例#2
0
    public void CreateNode(SplineComponent splineComponent)
    {
        GameObject controlPoint = new GameObject("ControlPoint");

        // Add the splineComponent component
        controlPoint.AddComponent <ControlPointComponent>();

        SplineCurve splineCurve    = splineComponent.GetSplineObject();
        Vector3     targetPosition = Vector3.one;

        Vector3[] controlPoints           = splineCurve.controlPoints;
        int       cpLength                = controlPoints.Length;
        int       controlPointsPerSegment = splineComponent.GetControlPointsPerSegment();

        if (cpLength >= controlPointsPerSegment)
        {
            // extrapolate the position from the last two points
            targetPosition = controlPoints[cpLength - 1] + (controlPoints[cpLength - 1] - controlPoints[cpLength - controlPointsPerSegment]);
        }
        controlPoint.transform.position = targetPosition;
        controlPoint.transform.parent   = splineComponent.transform;
    }