예제 #1
0
    // Update is called once per frame
    void Update()
    {
        Vector3      mousePos   = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2      mousePos2D = new Vector2(mousePos.x, mousePos.y);
        RaycastHit2D hit        = Physics2D.Raycast(mousePos2D, Vector2.zero);


        // check if collider is not a null
        if (hit.collider != null)
        {
            // if hovering over a minion
            if (hit.collider.gameObject.tag == "Minion" && hit.collider.gameObject.GetComponent <MinionTarget>().hired == false && holeController.candy >= hirePrice)
            {
                Cursor.SetCursor(cursorMinion, hotSpot, cursorMode);
                if (Input.GetMouseButtonDown(0))
                {
                    // get component of the minion clicked and make them hired
                    MinionTarget minionController = hit.collider.gameObject.GetComponent <MinionTarget>();
                    minionController.hired = true;
                    holeController.candy  -= hirePrice;
                    if (minionController.status == "hostage")
                    {
                        minionController.status = "roam";
                        minionController.changeTargets();
                    }
                    Debug.Log("Minion: " + hit.collider.gameObject.name + " is now hired");
                }
            }
            else if (hit.collider.gameObject.tag == "Enemy")
            {
                Cursor.SetCursor(cursorEnemy, hotSpot, cursorMode);
                if (Input.GetMouseButtonDown(0))
                {
                    MainControllerScript controller = GameObject.Find("MainCharacter").GetComponent <MainControllerScript>();
                    if (controller != null)
                    {
                        controller.launchProjectile(mousePos2D);
                    }
                }
            }
            else if (hit.collider.gameObject.tag == "Bowl" && hit.collider.gameObject.GetComponent <CandyBowlController>().collectable == true)
            {
                // bowl cursor
                Debug.Log("Mouse On Bowl");
                Cursor.SetCursor(cursorBowl, hotSpot, cursorMode);
                if (Input.GetMouseButtonDown(0))
                {
                    hit.collider.gameObject.GetComponent <CandyBowlController>().collectCandy();
                    GameObject.Find("MainCharacter").GetComponent <MainControllerScript>().playClip(clip);
                    Cursor.SetCursor(cursorDefault, hotSpot, cursorMode);
                }
            }
        }
        else
        {
            // default cursor
            Cursor.SetCursor(cursorDefault, hotSpot, cursorMode);
        }
    }
예제 #2
0
    void AimAndFire()
    {
        // Gun barrel rotation
        go_barrel.transform.Rotate(0, 0, currentRotationSpeed * Time.deltaTime);

        // if can fire turret activates
        if (canFire == true && go_target != null)
        {
            // start rotation
            currentRotationSpeed = barrelRotationSpeed;

            // aim at enemy
            Vector3 baseTargetPostition    = new Vector3(go_target.position.x, this.transform.position.y, go_target.position.z);
            Vector3 gunBodyTargetPostition = new Vector3(go_target.position.x, go_target.position.y, go_target.position.z);

            go_baseRotation.transform.LookAt(baseTargetPostition);
            go_GunBody.transform.LookAt(gunBodyTargetPostition);



            //Raycast to shoot
            RaycastHit hit;
            if (Physics.Raycast(go_baseRotation.position, go_baseRotation.transform.forward, out hit, 500f))
            {
                Debug.Log(hit.transform.name);

                MinionTarget target = hit.transform.GetComponent <MinionTarget>();

                if (target != null)
                {
                    target.takeDamage(turretDamage);
                }
                //impact effect to minion
                GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
                Destroy(impactGO, 0.4f);

                //Display gold get by destroying minion:
                //destroyedAmount++;

                //goldGained.text = destroyedAmount.ToString();
            }
            else
            {
                Debug.Log("Did not hit anything ");
            }
            //End of rayCast to Shoot


            // start particle system


            if (!muzzelFlash.isPlaying)
            {
                muzzelFlash.Play();
            }
        }
        else
        {
            // slow down barrel rotation and stop
            currentRotationSpeed = Mathf.Lerp(currentRotationSpeed, 0, 10 * Time.deltaTime);

            // stop the particle system
            if (muzzelFlash.isPlaying)
            {
                muzzelFlash.Stop();
            }
        }
    }