コード例 #1
0
    private void Move()
    {
        // already in the goal || have finished traveling
        if (pathLength == 0 || pathIndex == pathLength)
        {
            return;
        }

        // if we are close enough to the current pathIndex
        if (Vector3.Distance(Astar.GetWorldPosition(pathIndex), transform.position) < accuracy + SpawnOffsetY)
        {
            // if the current pathIndex is not the last index
            if (pathIndex != pathLength - 1)
            {
                pathIndex++;
            }
            else
            {
                //Player.LoseLife(Damage);
                damageStream.OnNext(this);
                isDead.Value = true;
            }
        }

        // if we are not at the end of the path
        if (pathIndex < pathLength)
        {
            goal = Astar.GetWorldPosition(pathIndex);
            Vector3 lookAtGoal = new Vector3(goal.x,
                                             transform.position.y,
                                             goal.z);

            Vector3    direction      = lookAtGoal - transform.position;
            Quaternion targetRotation = Quaternion.LookRotation(direction);

            // Look at the first path tile immediately
            if (pathIndex == 1)
            {
                transform.rotation = targetRotation;
            }

            transform.rotation = Quaternion.Slerp(transform.rotation,
                                                  targetRotation,
                                                  Time.deltaTime * turnSpeed);

            transform.Translate(0, 0, currentSpeed * Time.deltaTime);
        }
    }