Beispiel #1
0
    // Update is called once per frame
    private void Update()
    {
        EventSystem.current.SetSelectedGameObject(null);    // Disable button hover properly * requires using UnityEngine.EventSystems;
        if (!EventSystem.current.IsPointerOverGameObject()) // Prevents raycast from passing through UI * requires using UnityEngine.EventSystems;
        {
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            // Left mouse click (default move)
            if (Input.GetMouseButtonDown(0))
            {
                if (Physics.Raycast(ray, out hit, 100, movementMask) && hit.transform != null)
                {
                    wayPoint.transform.position = hit.point;
                    agent.destination           = hit.point;
                    RemoveFocus();
                }

                // If left mouse click is raycasted on an interactable gameobject, focus on the interactable
                if (Physics.Raycast(ray, out hit, 100) && hit.collider.tag == "Interactable" && hit.collider != null)
                {
                    Interactable interactable = hit.collider.GetComponent <Interactable>();

                    if (interactable != null)
                    {
                        SetFocus(interactable);
                        PrimaryAttack   = true;
                        SecondaryAttack = false;
                    }
                }
            }


            // Keypress Shift (freeze on spot)
            if (Input.GetKey(KeyCode.LeftShift))
            {
                if (Physics.Raycast(ray, out hit, 100, movementMask))
                {
                    if (hit.transform != null)
                    {
                        wayPoint.transform.position = hit.point;
                        agent.destination           = transform.position;
                        transform.LookAt(new Vector3(hit.point.x, transform.position.y, hit.point.z));

                        if (Input.GetMouseButtonDown(0))
                        {
                            // Does not work if you attack with keybinds
                            //combat.PrimarySkill(myStats);
                            //enemy.Interact();
                        }
                    }
                }
            }

            // Secondary skill
            if (Input.GetMouseButtonDown(1))
            {
                // If left mouse click is raycasted on an interactable gameobject, focus on the interactable
                if (Physics.Raycast(ray, out hit, 100) && hit.collider.tag == "Interactable" && hit.collider != null)
                {
                    Interactable interactable = hit.collider.GetComponent <Interactable>();

                    if (interactable != null)
                    {
                        SetFocus(interactable);
                        PrimaryAttack   = false;
                        SecondaryAttack = true;
                        //enemy.Interact();
                    }
                }
            }

            // Keypress I (inventory)
            if (Input.GetKeyDown(KeyCode.I))
            {
                combat.CheckInventory();
            }

            // Keypress M (map)
            if (Input.GetKeyDown(KeyCode.M))
            {
                combat.CheckMap();
            }

            // Check if we've reached the destination
            if (agent.remainingDistance > agent.stoppingDistance && wayPoint != null)
            {
                wayPoint.SetActive(true);
            }
            else
            {
                wayPoint.SetActive(false);
            }

            if (target != null)
            {
                agent.SetDestination(target.position);
                FaceTarget();
            }
        }
    }