Example #1
0
 void Decelerate()
 {
     if (deceleration > 0 && accelerators.Count == 0)
     {
         velocity = SloneUtil.AdvanceValue(velocity, Vector3.zero, deceleration);
     }
 }
Example #2
0
    // Update is called once per frame
    void FixedUpdate()
    {
        foreach (Vector3 acc in accelerators)
        {
            velocity += acc * Time.deltaTime;
        }

        if (topSpeed > 0)
        {
            velocity = SloneUtil.CapMagnitude(velocity, topSpeed);
        }

        Vector3 frameMove = velocity * Time.fixedDeltaTime;

        if (localMotion)
        {
            Vector3 forward = moverTransform.forward;
            Vector3 right   = moverTransform.right;
            Vector3 up      = moverTransform.up;
            moverTransform.position += ((right * frameMove.x) + (up * frameMove.y) + (forward * frameMove.z));
        }
        else
        {
            moverTransform.position += frameMove;
        }

        Decelerate();
    }
Example #3
0
    // Update is called once per frame
    void Update()
    {
        if (acceleration != 0f)
        {
            rotateVector = SloneUtil.AdvanceValue(rotateVector, goalRotateVector, acceleration);

            if (rotateVector == goalRotateVector)
            {
                acceleration = 0f;
            }
        }

        Vector3 finalRotation = rotateVector + randomizedRotation;

        if (localRotation)
        {
            moverTransform.Rotate(finalRotation * Time.deltaTime);
        }
        else
        {
            moverTransform.RotateAround(moverTransform.position, Vector3.right, finalRotation.x * Time.deltaTime);
            moverTransform.RotateAround(moverTransform.position, Vector3.up, finalRotation.y * Time.deltaTime);
            moverTransform.RotateAround(moverTransform.position, Vector3.forward, finalRotation.z * Time.deltaTime);
        }
    }
    // Advance the current position along the path according to the speed
    // and time that has passed.
    //
    protected override void AdvancePath(float speed, float deltaTime)
    {
        Vector3 pos = transform.position;

        float desiredDistance = speed * deltaTime;

        desiredDistance *= desiredDistance;

        while (pos.DistanceSquared(transform.position) < desiredDistance)
        {
            int pointIndex = Mathf.FloorToInt(t);
            if (pointIndex >= curve.pointCount - 1)
            {
                break;
            }

            float stepSize = desiredDistance / curveLengths [pointIndex];
            t += stepSize;

            pos = GetPosition(t);
        }

        t = Mathf.Clamp(t, 0.0f, (float)(curve.pointCount - 1));
        transform.position = SloneUtil.AdvanceValue(transform.position, pos, speed);
    }
Example #5
0
    IEnumerator Move_coroutine(Vector3 newPosition, float moveTime, bool smooth = true, bool localMovement = false)
    {
        Vector3 startPosition;

        if (localMovement)
        {
            startPosition = moverTransform.localPosition;
        }
        else
        {
            startPosition = moverTransform.position;
        }

        float startTime = Time.time;

        if (moveTime > 0)
        {
            while (startTime + moveTime > Time.time)
            {
                yield return(new WaitForFixedUpdate());

                float pct = (Time.time - startTime) / moveTime;

                if (localMovement)
                {
                    if (smooth)
                    {
                        moverTransform.localPosition = SloneUtil.LerpSmooth(startPosition, newPosition, pct);
                    }
                    else
                    {
                        moverTransform.localPosition = Vector3.Lerp(startPosition, newPosition, pct);
                    }
                }
                else
                {
                    if (smooth)
                    {
                        moverTransform.position = SloneUtil.LerpSmooth(startPosition, newPosition, pct);
                    }
                    else
                    {
                        moverTransform.position = Vector3.Lerp(startPosition, newPosition, pct);
                    }
                }
            }
        }

        if (localMovement)
        {
            moverTransform.localPosition = newPosition;
        }
        else
        {
            moverTransform.position = newPosition;
        }

        moveRoutine = null;
    }
Example #6
0
    // Update is called once per frame
    void Update()
    {
        float pct = 1.0f;

        if (changeTime > 0f)
        {
            pct = Mathf.Clamp01((Time.time - startTime) / changeTime);
        }

        sharedMaterialInstance.GetMaterialInstance().color = SloneUtil.Lerp(startColor, endColor, pct);
    }
Example #7
0
    void Update()
    {
        if (scaleSpeed > 0f)
        {
            moverTransform.localScale = SloneUtil.AdvanceValue(moverTransform.localScale, scaleGoal, scaleSpeed);

            // Once we reach it, stop advancing.
            if (moverTransform.localScale == scaleGoal)
            {
                scaleSpeed = 0f;
            }
        }
    }
Example #8
0
    IEnumerator Rotate_coroutine(Vector3 newAngles, float time, bool smooth = true, bool localMovement = true)
    {
        Vector3 startAngles;

        if (localMovement)
        {
            startAngles = moverTransform.localEulerAngles;
        }
        else
        {
            startAngles = moverTransform.eulerAngles;
        }

        float startTime = Time.time;

        if (rotateTime > 0)
        {
            while (startTime + rotateTime > Time.time)
            {
                yield return(new WaitForEndOfFrame());

                float pct = (Time.time - startTime) / rotateTime;

                if (smooth)
                {
                    pct = SloneUtil.LerpSmooth(0f, 1f, pct);
                }

                if (localMovement)
                {
                    moverTransform.localEulerAngles = SloneUtil.LerpEulerAngles(startAngles, newAngles, pct);
                }
                else
                {
                    moverTransform.eulerAngles = SloneUtil.LerpEulerAngles(startAngles, newAngles, pct);
                }
            }
        }

        if (localMovement)
        {
            moverTransform.localEulerAngles = newAngles;
        }
        else
        {
            moverTransform.eulerAngles = newAngles;
        }
    }
Example #9
0
    IEnumerator Rotate_coroutine(Vector3 newRotation, float moveTime, bool localMovement = false)
    {
        Vector3 startRotation;

        if (localMovement)
        {
            startRotation = moverTransform.localEulerAngles;
        }
        else
        {
            startRotation = moverTransform.eulerAngles;
        }

        float startTime = Time.time;

        if (moveTime > 0)
        {
            while (startTime + moveTime > Time.time)
            {
                yield return(new WaitForFixedUpdate());

                float pct = (Time.time - startTime) / moveTime;
                if (localMovement)
                {
                    moverTransform.localEulerAngles = SloneUtil.LerpEulerAngles(startRotation, newRotation, pct);
                }
                else
                {
                    moverTransform.eulerAngles = SloneUtil.LerpEulerAngles(startRotation, newRotation, pct);
                }
            }
        }

        if (localMovement)
        {
            moverTransform.localEulerAngles = newRotation;
        }
        else
        {
            moverTransform.eulerAngles = newRotation;
        }

        rotateRoutine = null;
    }
Example #10
0
    // Get the world-space vector from this point to the nearest point onscreen that maintains
    // the current distance from the camera.
    //
    // position: position to check.
    // cam: camera on which to check (Deaults to main camera).
    //
    public static Vector2 GetVectorToOnscreen(Vector3 position, Camera cam = null)
    {
        if (cam == null)
        {
            cam = Camera.main;
        }

        Vector3 toPosition = position - cam.transform.position;

        float zDist = Vector3.Dot(toPosition, cam.transform.forward);

        if (zDist < 0)
        {
            Debug.LogError("Cannot find distance offscreen for object behind camera.");
            return(Vector2.zero);
        }

        // Get the position relative to the camera.
        float xDist = Vector3.Dot(toPosition, cam.transform.right);
        float yDist = Vector3.Dot(toPosition, cam.transform.up);

        Vector2 halfViewportSize = SloneUtil.GetViewportSizeAtDistance(zDist, cam) * 0.5f;

        // Calculate the distance from each camera-relative direction (x and y) and multiply back into world space.
        //
        Vector2 returnX = Vector2.zero;

        if (Mathf.Abs(xDist) > halfViewportSize.x)
        {
            float xOffscreen = -1f * Mathf.Sign(xDist) * (Mathf.Abs(xDist) - halfViewportSize.x);
            returnX = xOffscreen * cam.transform.right;
        }

        Vector2 returnY = Vector2.zero;

        if (Mathf.Abs(yDist) > halfViewportSize.y)
        {
            float yOffscreen = -1f * Mathf.Sign(yDist) * (Mathf.Abs(yDist) - halfViewportSize.y);
            returnY = yOffscreen * cam.transform.up;
        }

        return(returnX + returnY);
    }
Example #11
0
    // Evaluate the oscillator as a curve with output between 0 and 1.
    //
    private float Evaluate01()
    {
        // Multiply by 2 so that 50% yields 1.0 and 100% is back to 0.0.
        float pct = ((Time.time - startTime) / wavelength) * 2f;

        if (curveType == OscillationType.SMOOTH)
        {
            return(SloneUtil.LerpSmooth(0f, 1f, pct, true));
        }
        else
        {
            if (curveType != OscillationType.LINEAR)
            {
                Debug.LogError("Unknown OscillationType: " + curveType);
            }

            int   baseNumber  = Mathf.FloorToInt(pct);
            bool  downSwing   = (baseNumber % 2) == 1;
            float leftoverPct = pct - ((float)baseNumber);
            return(downSwing ? 1.0f - leftoverPct : leftoverPct);
        }
    }
    // Advance the rotation of the camera toward the specified goal.
    //
    // goalAngle: Angle (in degrees, 0-360) toward which to rotate the camera.
    //
    void UpdateRotation(float goalAngle)
    {
        Vector3 angles = Camera.main.transform.eulerAngles;

        Camera.main.transform.eulerAngles = new Vector3(angles.x, angles.y, SloneUtil.AdvanceAngle(angles.z, goalAngle, rotationChangeSpeed));
    }
 // Advance the field of view toward the specified goal.
 //
 // goalFov: the desired final FOV.
 //
 void UpdateFOV(float goalFov)
 {
     Camera.main.fieldOfView = SloneUtil.AdvanceValue(Camera.main.fieldOfView, goalFov, fovChangeSpeed);
 }
Example #14
0
    // Lerp from one vector to another using a smoothed lerp to ease departure and approach.
    //
    // from: starting value, returned if pct is 0.0
    // to: ending value, returned if pct is 1.0
    // pct: percentage (0.0-1.0) along the continuum between from and to
    // oscillate: True if values beyond 0-1 should oscillate back to where they started.
    //
    public static Vector3 LerpSmooth(Vector3 from, Vector3 to, float pct, bool oscillate = false)
    {
        float smoothed = SmoothValue(pct, oscillate);

        return(SloneUtil.LerpUnbounded(from, to, smoothed));
    }