コード例 #1
0
    // Update is called once per frame
    void Update()
    {
        if (atDestination) //stops all movement once destination is reached
        {
            return;
        }
        else
        {
            if (Time.time > lastRepath + repathRate && seeker.IsDone())
            {
                lastRepath = Time.time;

                //Start a new path to the targetPosition, call the OnPathComplete function
                //when the path has been calculated (could take a few frames)
                seeker.StartPath(transform.position, targetPosition.position, OnPathComplete);
            }

            if (path == null)
            {
                //no active path yet, so do nothing
                return;
            }

            if (currentWaypoint > path.vectorPath.Count)
            {
                return;
            }

            if (currentWaypoint == path.vectorPath.Count)
            {
                OnDestinationReached();
                currentWaypoint++;
                return;
            }

            //Direction to the next waypoint, normalized so it has the length of 1 world unit
            Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position).normalized;

            //multiply with speed to get velocity
            Vector3 velocity = dir * stats.GetMovementSpeed();

            //SimpleMove takes velocity in m/s, so no need to multiply by time.DeltaTime
            controller.SimpleMove(velocity);

            if (dir != Vector3.zero) //rotates the model towards the direction of movement
            {
                transform.rotation = Quaternion.Slerp(
                    transform.rotation,
                    Quaternion.LookRotation(dir),
                    Time.deltaTime * stats.GetMovementSpeed()
                    );
            }

            if ((transform.position - path.vectorPath[currentWaypoint]).sqrMagnitude < nextWayPointDistance * nextWayPointDistance)
            {
                currentWaypoint++;
                return;
            }
        }
    }
コード例 #2
0
    public void SelectMinion(GameObject _minion)
    {
        buildingUI.SetActive(false);
        selectedObject = _minion;
        workerStats    = selectedObject.GetComponent <WorkerStats>();

        minionName.text         = workerStats.GetName();
        minionSpeed.text        = workerStats.GetMovementSpeed().ToString();
        minionProductivity.text = workerStats.GetProductivity().ToString();
        minionWood.text         = workerStats.GetWood().ToString();
        minionStone.text        = workerStats.GetStone().ToString();

        minionUI.SetActive(true);
    }