Exemple #1
0
    private void Update()
    {
        //目的地に到着していない場合
        if (Vector3.Distance(transform.position, destination) > 0.5f)
        {
            direction = (destination - transform.position).normalized;

            //通常移動
            if (impact == false)
            {
                controller.Move(direction * speed);
                transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.FromToRotation(Vector3.up, direction), 0.01f);
            }
            //加速移動
            else if (impact == true)
            {
                direction = (destination - transform.position).normalized;

                controller.Move(direction * impactSpeed);
                if (impactSpeed >= 0.03)
                {
                    impactSpeed -= 0.001f;
                    Debug.Log(impactSpeed);
                }
                transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.FromToRotation(Vector3.up, direction), 0.1f);
            }
            //タッチ箇所への移動
            if (Input.GetMouseButtonDown(0))
            {
                Ray();
            }
        }

        //目的地に着いた場合
        else
        {
            impact       = false;
            impactSpeed  = 0.15f;
            elapsedTime += Time.deltaTime;
            if (elapsedTime >= waitTime)
            {
                setPosition.CreateRandomPosition();
                destination = setPosition.GetDestenation();
                elapsedTime = 0f;
            }

            if (Input.GetMouseButtonDown(0))
            {
                Ray();
            }
        }

        //エサがあるとき、エサを追いかける
        if (foodnullor)
        {
            foods = GameObject.FindGameObjectsWithTag("Food");
            if (foods.Length != 0)
            {
                destination = foods[0].transform.position;
            }
            else if (foods.Length == 0)
            {
                foodnullor = false;
            }
        }

        //タッチ処理
        void Ray()
        {
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            int        layerMask = (1 << LayerMask.NameToLayer("HitPlane"));

            if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
            {
                Vector3 pos = new Vector3(hit.point.x, hit.point.y, transform.position.z);
                if (impact == true)
                {
                    impactSpeed = 0.15f;
                    impact      = false;
                }
                if (buttonControler.foodButton)
                {
                    //エサの生成
                    Instantiate(food, pos, Quaternion.identity);
                    foodnullor = true;
                }
                else
                {
                    //波紋エフェクトの生成
                    Instantiate(touchEffectPrefab, pos, Quaternion.identity);
                    destination = pos;
                }
            }
        }
    }