Example #1
0
    //public void UpdatePlayer()
    void Update()
    {
        //if (startFlag == false) return;

        // WASD入力から、XZ平面(水平な地面)を移動する方向(velocity)を得ます
        velocity = Vector3.zero;
        if (moveFlag == true)
        {
            if (Input.GetKey(KeyCode.W))
            {
                velocity.z += 1;
            }
            if (Input.GetKey(KeyCode.A))
            {
                velocity.x -= 1;
            }
            if (Input.GetKey(KeyCode.S))
            {
                velocity.z -= 1;
            }
            if (Input.GetKey(KeyCode.D))
            {
                velocity.x += 1;
            }

            if (Input.GetKey(KeyCode.LeftShift) && speedUpRecastFlag == false)
            {
                speedUoFlag = true;

                // スピードアップする
                velocity = velocity.normalized * 1.5f;
            }
            // スピードアップがリキャストタイムに入った場合
            else if (speedUpRecastFlag == true)
            {
                //
                sppedUpTime        = 0f;
                speedUpRecastTime += Time.deltaTime;
                gm.GetSpeedUpGaugeUI().fillAmount = speedUpRecastTime / maxspeedUpRecastTime;
                if (speedUpRecastTime > maxspeedUpRecastTime)
                {
                    speedUpRecastFlag = false;
                    speedUpRecastTime = 0f;
                }
                velocity = velocity.normalized;
            }
            else
            {
                if (gm.GetSpeedUpGaugeUI().fillAmount > 0f && speedUoFlag == true)
                {
                    gm.GetSpeedUpGaugeUI().fillAmount -= 1f / maxSpeedUpTime * Time.deltaTime;
                }
                velocity = velocity.normalized;
            }

            if (speedUoFlag == true)
            {
                // スピードアップしている時間を計測
                sppedUpTime += Time.deltaTime;

                // 上限時間までスピードアップした場合はスピードアップを止めてリキャストに入る
                if (sppedUpTime > maxSpeedUpTime)
                {
                    speedUpRecastFlag = true;
                    speedUoFlag       = false;
                }

                gm.GetSpeedUpGaugeUI().fillAmount -= 1f / maxSpeedUpTime * Time.deltaTime;
            }
            //velocity = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
        }

        // いずれかの方向に移動している場合
        if (velocity.magnitude > 0)
        {
            // プレイヤーの位置(transform.position)の更新
            // 移動方向ベクトル(velocity)を足し込みます
            //this.transform.position = Vector3.MoveTowards(this.transform.position, this.transform.position + velocity.normalized, moveSpeed * Time.deltaTime);
            this.transform.position += velocity * moveSpeed * Time.deltaTime;

            // キーボードの方向キーの移動量を取得し、その移動量を前の値から新しい値へと補間した状態をvelocityに入れ直す
            velocity     = Vector3.MoveTowards(oldVeclocity, velocity, moveSpeed * Time.deltaTime);
            oldVeclocity = velocity;

            this.transform.LookAt(transform.position + velocity);

            LeftTrail();
        }

        float angle = LookCanon();

        // 弾の発射タイミングを管理するタイマーを更新する
        m_shotTimer += Time.deltaTime;

        // マウスクリック左で弾を発射
        if (Input.GetMouseButtonDown(0))
        {
            // 発射間隔と残弾数のチェック
            if (m_shotTimer >= m_shotInterval && shotNum > 0)
            {
                // 弾を発射する
                ShootNWay(angle, m_shotAngleRange, m_shotSpeed, m_shotCount);

                // 持ち弾を減らす
                shotNum--;
                psg.SetShotGauge(shotNum);

                // 弾の発射タイミングを管理するタイマーをリセットする
                m_shotTimer = 0;

                // 近くに敵がいた場合はその敵のステートを攻撃にする
                if (GameMgr.enemyList != null)
                {
                    foreach (Enemy e in GameMgr.enemyList)
                    {
                        if ((this.transform.position - e.transform.position).sqrMagnitude < e.GetNormalEyeSightLength())
                        {
                            if (e.GetStates() != StateType.Danger)
                            {
                                e.SetRelayStagePos(Vector3.zero);
                                e.SetTargetStagePos(this.transform.position);
                                e.SetStates(StateType.Caution);
                            }
                        }
                    }
                }

                StartCoroutine("Stun");
            }
        }

        // マウスクリックでデコイを配置
        if (Input.GetMouseButtonDown(1))
        {
            // デコイ数をチェック
            // マウスクリックした場所から近いステージにデコイを配置する
            if (decoyNum > 0)
            {
                // メインカメラからクリックした地点のベクトルでRayを飛ばす
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                //
                RaycastHit hit = new RaycastHit();

                //レイを投げて何かのオブジェクトに当たった場合
                if (Physics.Raycast(ray, out hit))
                {
                    // 衝突したオブジェクトがステージかウェイポイントの場合はデコイ配置
                    if (hit.collider.tag == "Stage" || hit.collider.tag == "Point")
                    {
                        //レイが当たった位置(hit.point)にオブジェクトを生成する
                        currentPosition   = hit.point;
                        currentPosition.y = 0;

                        Decoy decoy = Decoy.Add(GameMgr.decoyList.Count, gm, currentPosition);
                        decoy.InitilizeObjColor();
                        GameMgr.decoyList.Add(decoy);
                        decoyNum--;
                        dg.SetDecoyGauge(decoyNum);

                        decoy.StaetRemoveDecoyCoroutine();
                    }
                }
            }
        }
    }