// Update is called once per frame
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.N))
     {
         SpawnUnit();
     }
     if (Input.GetKeyDown(KeyCode.Space))
     {
     }
     if (Input.GetMouseButtonDown(0))
     {
         Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit))
         {
             if (hit.collider.gameObject.GetComponent <MeshCollider>())
             {
                 Vector3 newLocation = hit.point;
                 Node    newNode     = connectionGrid.GetNodeFromLocation(newLocation);
                 if (newNode != null)
                 {
                     MoveUnits(newNode);
                 }
             }
         }
     }
 }
示例#2
0
    public Node GetPlayerNodeLocation()
    {
        Vector3 playerLocation = player.gameObject.transform.position;
        Node    newNode        = connectionGrid.GetNodeFromLocation(playerLocation);

        return(newNode);
    }
    private void UpdateMovement()
    {
        navUpdateTimer++;


        if (navUpdateTimer >= navUpdateCounter)
        {
            navUpdateTimer = 0;
            Node newDestination = mainGrid.GetPlayerLocation();
            GetPath(newDestination);
        }
        if (transform.position.y < -10)
        {
            DeactivateAgent();
        }

        if (rB != null)
        {
            currentVelocity  = rB.velocity;
            currentMagnitude = currentVelocity.magnitude;
            if (currentMagnitude < .1f && !isGrounded)
            {
                ToggleIsGrounded(true);
            }
        }

        if (okToMove && agentPath != null && agentPath.Count > 0)
        {
            currentNode      = agentPath[0];
            distFromNextNode = Vector3.Distance(transform.position, currentNode.GetLocation());

            switchNodeDist = (rB.useGravity && !isGrounded) ? jumpSwitchDist : moveSwitchDist;

            if (distFromNextNode < switchNodeDist)
            {
                lastNode = agentPath[0];

                //Make GridAgent Jump to another Grid
                if (agentPath.Count > 1 && lastNode.gridParent != agentPath[1].gridParent)
                {
                    waitCounter = 0;
                    float airTime = 2f;// Random.Range(minAirTime, maxAirTime);
                    Launch(agentPath[1].GetLocation(), airTime);
                }

                agentPath.RemoveAt(0);
                //GridAgent has reached Destination
                if (agentPath.Count < 1)
                {
                    ToggleOkToMove(false);
                    ResetVelocity();
                    ToggleGravity(true);
                }
            }
            else if (distFromNextNode >= resetPathDist && isGrounded && currentMagnitude == 0)
            {
                //Debug.Log("Need to Find a new Path");
                Node newEndNode = agentPath[agentPath.Count - 1];
                Node startNode  = mainGrid.GetNodeFromLocation(transform.position);
                if (startNode != null && startNode.gridParent == newEndNode.gridParent)
                {
                    GetPath(newEndNode);
                }
            }

            //HandleRotation
            if (isGrounded)
            {
                if (currentNode != null)
                {
                    Vector3 newDirection = ((currentNode.GetLocation() - transform.position).normalized);
                    rB.MovePosition(transform.position + newDirection * movementSpeed * Time.deltaTime);
                    Quaternion newRotation       = Quaternion.LookRotation(newDirection);
                    Quaternion newSmoothRotation = Quaternion.Slerp(transform.rotation, newRotation, rotationSpeed * Time.deltaTime);
                    rB.MoveRotation(newSmoothRotation);

                    waitCounter = 0;
                }
            }
        }
        if (gameManager)
        {
            distFromPlayer = Vector3.Distance(transform.position, gameManager.GetPlayerLocation());
            if (distFromPlayer > minDistFromPlayer && currentMagnitude < .05f)
            {
                waitCounter++;
                if (waitCounter >= waitTime)
                {
                    waitCounter = 0;
                    Launch(gameManager.GetPlayerLocation(), 2);
                }
            }
        }
    }