コード例 #1
0
    void Update()
    {
        // Check if the bee is currently stunned.
        if (stunned)
        {
            // A stunned bee will shrink and spiral down.
            stunnedLerp = Mathf.Lerp(stunnedLerp, 0f, Time.deltaTime);
            transform.Rotate(new Vector3(0f, 0f, 360f * Time.deltaTime));
            transform.localScale = Vector3.one * stunnedLerp;

            // Check if we are done with the stunned animation.
            if (stunnedLerp < 0.1f)
            {
                // Deactivate the bee.
                Terminate();
            }
        }
        else
        {
            //Check if the bee has arrived at waypoint
            Vector3 waypoint;

            if (flightPath.GetWaypoint(out waypoint))
            {
                //If we have a waypoint, update current velocity to move towards it
                velocity = Vector3.Normalize(waypoint - transform.position) * moveSpeed;
            }
            //Update the bee's position (i.e. displacement = velocity*time)
            transform.position += velocity * Time.deltaTime;

            //Update the bee's rotation
            Quaternion newRotation = Quaternion.FromToRotation(Vector3.up, velocity);
            transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime);

            //Finally, we make sure the bee dosen't wander off screen
            KeepBeeWithinViewport();

            //Check if the bee has reached the waypoint
            if (flightPath.HasArrived())
            {
                //Deactivate the bee
                Terminate();
            }
        }
    }