// Update is called once per frame
        void Update()
        {
            //When you click while playing
            if (Input.GetMouseButtonDown(0) && Time.timeScale == 1)
            {
                //Throw a raycast from the cursor through the screen
                RaycastHit2D[] hit = Physics2D.LinecastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Camera.main.ScreenToWorldPoint(Input.mousePosition));

                //If at least one collider was hit
                if (hit.Length > 0)
                {
                    //Sorting algorithm for top rendered sprite (may need to rework in future if it doesn't work properly)
                    int topHit   = 0;
                    int preValue = hit[0].transform.GetComponent <SpriteRenderer>().sortingLayerID;

                    for (int arrayID = 1; arrayID < hit.Length; arrayID++)
                    {
                        int tempValue = hit[arrayID].transform.GetComponent <SpriteRenderer>().sortingLayerID;
                        if (tempValue > preValue)
                        {
                            preValue = tempValue;
                            topHit   = arrayID;
                        }
                    }
                    //Debug prints
                    //print(topHit);
                    //print(Time.frameCount);

                    //Assign the selected object to the target variable
                    clickTarget = hit[topHit].collider.transform.parent.gameObject;
                    script      = clickTarget.GetComponent <PoofScript>();

                    //Check the tag on the deleted object and change the appropriate score in the spawner script
                    //Also, initiate destruction method for target
                    if (script.isDead == false)
                    {
                        if (clickTarget.tag == "Sheep")
                        {
                            script.Poof();
                            spawnscript.realSheepCount -= 1;
                            realSheepYeeted            += 1;
                        }

                        if (clickTarget.tag == "Rat" || clickTarget.tag == "Wolf" || clickTarget.tag == "Cat" || clickTarget.tag == "Rock")
                        {
                            script.Poof();
                            spawnscript.fakeSheepCount -= 1;
                            fakeSheepYeeted            += 1;
                        }
                    }
                }
            }
        }
Beispiel #2
0
        float wandertimermax;           //the upper limit for waiting

        void Start()
        {
            //xPos = transform.position.x;
            //yPos = transform.position.y;
            //newDir();

            //Assign tilemap and grid from level
            tileMap = GameObject.Find("Tilemap").GetComponent <Tilemap>();
            grid    = GameObject.Find("Grid").GetComponent <Grid>();

            //Start sheep in wandering mode to start movement loop
            //wandering = true;
            //print("script start");

            //Start entity with no mode active, to wait for birth animation to play
            wandering = false;
            moving    = false;
            waiting   = false;

            //Assign prefab's poof script
            poofScript = GetComponent <PoofScript>();

            //Initializing variables (adjust as necessary)
            wanderlengthmin = 0.5f;
            wanderlengthmax = 4f;
            speed           = 0.2f;
            wandertimermax  = 4;

            //Set initial stopwatch for first waiting phase
            stopwatch = 0;

            //Set the timer for the waiting method with altered values for first waiting
            wandertimer = Random.Range(0.5f, 1.25f);

            //Assign child object's sprite renderer
            sprite         = transform.GetChild(0).gameObject;
            spriteRenderer = sprite.GetComponent <SpriteRenderer>();
        }