void Update() { // 照準を処理する _crosshairImage.rectTransform.position = Input.mousePosition; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, _rangeDistance)) { _gunObject.transform.LookAt(hit.point); // 銃の方向を変えている } // 敵が照準に入っているか調べる bool isEnemyTargeted = Physics.Raycast(ray, out hit, _rangeDistance, _enemyLayer); _crosshairImage.color = isEnemyTargeted ? _colorFocus : _colorNormal; // 三項演算子 ? を使っている _currentTargetEnemy = isEnemyTargeted ? hit.collider.gameObject.GetComponent <GunEnemyController>() : null; // 三項演算子 ? を使っている // 左クリック入力時の処理 if (Input.GetButtonDown("Fire1")) { _onShoot.Invoke(); // 敵に当たったら得点を足して表示を更新する if (_currentTargetEnemy) { _currentTargetEnemy.Hit(); } } }
void Update() { m_crosshairImage.rectTransform.position = Input.mousePosition; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, m_rangeDistance)) { m_gunObject.transform.LookAt(hit.point); // 銃の方向を変えている } // 敵が照準に入っているか調べる bool isEnemyTargeted = Physics.Raycast(ray, out hit, m_rangeDistance, m_enemyLayer); m_crosshairImage.color = isEnemyTargeted ? m_colorFocus : m_colorNormal; // 三項演算子 ? を使っている m_currentTargetEnemy = isEnemyTargeted ? hit.collider.gameObject.GetComponent <GunEnemyController>() : null; // 三項演算子 ? を使っている if (Input.GetButtonDown("Fire1")) { m_onShoot.Invoke(); // 敵に当たったら得点を足して表示を更新する if (m_currentTargetEnemy) { m_score += m_currentTargetEnemy.Hit(); if (m_score > m_nextLifeUpScore) { m_nextLifeUpScore += m_scoreIntervalForLifeUp; m_onLifeUp.Invoke(); } RefreshScore(); } } }