Пример #1
0
    //Controlamos que nos podemos mover hacia donde pulsamos
    bool Blocked(Vector3 position, Vector2 direction)
    {
        Vector2 newPos = new Vector2(position.x, position.y) + direction;

        GameObject[] walls = GameObject.FindGameObjectsWithTag("Wall");
        foreach (var wall in walls)
        {
            if (wall.transform.position.x == newPos.x && wall.transform.position.y == newPos.y)
            {
                return(true);
            }
        }
        GameObject[] boxes = GameObject.FindGameObjectsWithTag("Box");
        foreach (var box in boxes)
        {
            if (box.transform.position.x == newPos.x && box.transform.position.y == newPos.y)
            {
                BoxController bx = box.GetComponent <BoxController>();
                if (bx && bx.Move(direction))
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
        }
        return(false);
    }
Пример #2
0
    void MoveInput()
    {
        //jump if character is grounded and Jump key is pressed
        if (Input.GetButton(KeyJump))
        {
            mBox.Jump();
        }
        if (Input.GetButtonDown(KeyRoll))
        {
            mBox.Roll();
        }
        if (Input.GetButtonDown(KeyItem))
        {
            mBox.UseItem();
        }


        ///////////////////////////////////
        h = 0;
        v = 0;
        if (!mBox.acting)
        {
            h = Input.GetAxis(KeyHorizontal);
            v = Input.GetAxis(KeyVertical);
        }
        bool tempkeyHAxis = (h > 0.0f);
        bool tempkeyVAxis = (v > 0.0f);

        //if(resettingCamera && (Mathf.Abs(h-tempH) > 0.1f || Mathf.Abs(v-tempV) > 0.1f))
        if (resettingCamera && ((keyH != Input.GetButton(KeyHorizontal) || keyV != Input.GetButton(KeyVertical)) ||
                                (tempkeyHAxis != keyHAxis || tempkeyVAxis != keyVAxis)
                                ))
        {
            resettingCamera = false;
            Debug.Log("resettingCamera:: false");
            //h = 0.0f;
            //v = 0.0f;
        }

        //calculate move vector and pass to boxcontroller
        if (mCamera != null)
        {
            if (!resettingCamera)
            {
                cameraFoward = Vector3.Scale(mCamera.forward, new Vector3(1, 0, 1)).normalized;
                cameraRight  = Vector3.Scale(mCamera.right, new Vector3(1, 0, 1)).normalized;
            }
            moveVector = v * cameraFoward + h * cameraRight;
        }
        else
        {
            moveVector = v * Vector3.forward + h * Vector3.right;
        }
        mBox.Move(moveVector, Input.GetButton(KeySprint));

        //////////////////////////////////
    }
Пример #3
0
    void FixedUpdate()
    {
        if (gameObject.GetComponent <BoxController>().acting)
        {
            return;
        }

        if (!isChase)
        {
            timeCount += Time.fixedDeltaTime;
        }

        timeCountChase += Time.fixedDeltaTime;
        //Debug.Log(timeCountChase);
        Quaternion temp = transform.rotation;


        isGrounded = false;

        float minDis = 1000000;

        Collider[] toFind = Physics.OverlapSphere(transform.position, atkRange);
        GameObject tempChase;

        if (timeCountChase >= chaseWait || isChase)
        {
            foreach (Collider c in toFind)
            {
                if (c.gameObject.tag == "Player" && c.gameObject != mother)
                {
                    float dis = Vector3.Distance(c.gameObject.transform.position, transform.position);
                    if (dis < minDis && dis <= 5.0f)
                    {
                        //Debug.Log(c.gameObject.tag);
                        minDis = dis;
                        if (!isChase)
                        {
                            isChase = true;
                            chasing = c.gameObject;
                        }
                    }
                }
            }
            if (minDis >= 1000000)
            {
                isChase = false;
                chasing = null;
            }
            attackCDCount += Time.fixedDeltaTime;
            GoAttack();
            timeCountChase = 0;
        }

        if (!isChase)
        {
            if (!isRoaming && timeCount >= randomWait)
            {
                bool flag      = false;
                int  tempCount = 0;
                while (!flag && tempCount < 30)
                {
                    roamingVec = new Vector3(((float)Random.Range(-100, 100)) / 100.0f, 0, ((float)Random.Range(-100, 100)) / 100.0f);
                    roamingVec.Normalize();
                    transform.LookAt(transform.position + roamingVec);
                    flag = CheckGrounded();
                    //  Debug.Log("GGGGG");
                    tempCount++;
                }

                if (tempCount >= 30)
                {
                    return;
                }



                randomMove = ((float)Random.Range(300, 800)) / 100.0f;
                isRoaming  = true;
                timeCount  = 0;
            }

            if (isRoaming && timeCount >= randomMove)
            {
                isRoaming  = false;
                roamingVec = Vector3.zero;
                randomWait = ((float)Random.Range(1000, 4000)) / 1000.0f;
                timeCount  = 0;
            }

            if (isRoaming)
            {
                bool flag = CheckGrounded();
                if (!flag)
                {
                    isRoaming  = false;
                    roamingVec = Vector3.zero;
                    timeCount  = 0;
                    randomWait = ((float)Random.Range(1000, 2000)) / 1000.0f;
                }
            }

            Transform  checkWall = transform.FindChild("checkWall");
            Collider[] toCheck   = Physics.OverlapSphere(checkWall.position, 0.01f);
            isHitWall = false;

            foreach (Collider c in toCheck)
            {
                // Debug.Log(c.transform.tag);
                if (c.transform.tag != "Monster" && c.transform.tag != "Player" && c.transform.tag != "PlayerGroundCheck" && !c.isTrigger)
                {
                    isHitWall = true;
                }
            }

            if (isHitWall)
            {
                roamingVec = roamingVec * -1;
            }


            if (isGrounded)
            {
                transform.rotation = temp;
            }
        }
        else if (isChase)
        {
            isRoaming  = false;
            timeCount  = 0;
            randomWait = ((float)Random.Range(1000, 2000)) / 1000.0f;
            roamingVec = chasing.transform.position - transform.position;
            roamingVec.Normalize();

            float d = Vector3.Distance(chasing.transform.position, transform.position);
            if (d <= 0.8f)
            {
                roamingVec = Vector3.zero;
            }
        }
        // Debug.Log("roaming");
        roamingVec = new Vector3(roamingVec.x, 0, roamingVec.z);
        if (isChase)
        {
            monster.Move(roamingVec, true);
        }
        else
        {
            monster.Move(roamingVec, false);
        }
    }