private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            _jumpController.Jump();             // Make the player jump when "Space" is pressed
        }

        _controller.IsRunning = Input.GetKey(KeyCode.LeftShift);            // Make player run when "Left Shift" is being held

        // Get the movement direction from WASD and Arrow keys
        Vector2 MoveDir = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));

        _controller.Move(MoveDir);              // move player in a specified direction
        _controller.Rotate(MoveDir);            // make player look in a specified direction
    }
Exemple #2
0
    // Executed each frame
    private void Update()
    {
        _controller.CanMove = (IsTargeting);            // robot can move unless it's not pursing its target

        // calculate a path base don target's current position
        _agent.CalculatePath(new Vector3(FollowTarget.position.x, 0, FollowTarget.position.z), path);

        if (path.corners.Length == 1)           // if robot can walk up to its target with no obstacles to avoid
        {
            // set its move direction towards its target
            moveDir = (FollowTarget.position - transform.position).normalized;
        }
        else if (path.corners.Length > 1)       // if robot has not pass at least one abstracle
        {
            // set its move direction towards next path corner
            moveDir = (path.corners[1] - transform.position).normalized;
        }

        // move and rotate robot based on directions calculated
        _controller.Move(new Vector2(moveDir.x, moveDir.z));
        _controller.Rotate(new Vector2(moveDir.x, moveDir.z));

        AttackTarget();     // attacks target if within its reach
    }