- Unity Artificial Intelligence Programming
- Dr. Davide Aversa Aung Sithu Kyaw Clifford Peters
- 132字
- 2021-06-10 18:57:50
The Attack state
If the player tank is close enough to attack our AI tank, we will rotate the turret object to face the player tank, and then start shooting. Then, if the player tank goes out of range, the tank goes back to the Patrol state:
protected void UpdateAttackState() { //Set the target position as the player position destPos = playerTransform.position; //Check the distance with the player tank float dist = Vector3.Distance(transform.position, playerTransform.position); if (dist >= 200.0f && dist < 300.0f) { //Rotate to the target point Quaternion targetRotation = Quaternion.LookRotation(destPos - transform.position); transform.rotation = Quaternion.Slerp( transform.rotation, targetRotation, Time.deltaTime * curRotSpeed); //Go Forward transform.Translate(Vector3.forward * Time.deltaTime * curSpeed); curState = FSMState.Attack; } //Transition to patrol is the tank become too far else if (dist >= 300.0f) { curState = FSMState.Patrol; } //Always turn the turret toward the player Quaternion turretRotation = Quaternion.LookRotation(destPos - turret.position); turret.rotation = Quaternion.Slerp(turret.rotation, turretRotation, Time.deltaTime * curRotSpeed); //Shoot the bullets ShootBullet(); } private void ShootBullet() { if (elapsedTime >= shootRate) { //Shoot the bullet Instantiate(Bullet, bulletSpawnPoint.position, bulletSpawnPoint.rotation); elapsedTime = 0.0f; } }
推薦閱讀
- 物聯(lián)網(wǎng)與北斗應(yīng)用
- 智能網(wǎng)聯(lián)汽車V2X與智能網(wǎng)聯(lián)設(shè)施I2X
- 電子政務(wù)效益的經(jīng)濟分析與評價
- 中小型局域網(wǎng)組建、管理與維護實戰(zhàn)
- 2小時讀懂物聯(lián)網(wǎng)
- 面向5G-Advanced的關(guān)鍵技術(shù)
- 網(wǎng)絡(luò)安全應(yīng)急響應(yīng)技術(shù)實戰(zhàn)指南
- 語音信號處理及Blackfin DSP實現(xiàn)
- 5G+區(qū)塊鏈
- Hands-On Docker for Microservices with Python
- Web用戶查詢?nèi)罩就诰蚺c應(yīng)用
- Getting Started with tmux
- 物聯(lián)網(wǎng)M2M開發(fā)技術(shù):基于無線CPU-Q26XX
- 物聯(lián)網(wǎng),So Easy!
- Cisco無線局域網(wǎng)配置基礎(chǔ)