当前位置:网站首页>Unity intelligent NPC production -- pre judgment walking (method 1)
Unity intelligent NPC production -- pre judgment walking (method 1)
2022-07-05 04:55:00 【yoyoHm】
1. Judge the pre walk position according to the player's orientation
2. Launch target point calculation
3. Set the emission direction
( Player pre position = Players face normalization + Player position )
Vector3 f = Vector3.Normalize(playerTransform.forward);// normalized(playerTransform.forward); //playerTransform.forward.
int speed = playerContoller.IsStop == true ? 0 :2;
Quaternion newRotation = Quaternion.LookRotation(playerTransform.position+f*speed - transform.position);
turret.rotation = Quaternion.Slerp(turret.rotation, newRotation, Time.deltaTime * 10f);
complete NPC Code
//HM===== Time :
// Design content :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyTank : FSM {
public enum FSMStates
{
Patrol,
Chase,
Attack,
Dead
}
// Use this for initialization
[SerializeField] private FSMStates currentState = FSMStates.Patrol;
[SerializeField] private GameObject enemyTankTurret;
[SerializeField] private Transform enemyTankBulletSpawnPos;
[SerializeField] private GameObject bulletPrefab;
[SerializeField] private int health = 10;
private PlayerContoller playerContoller;
public int CurrentHealth { get; set; }
private Transform turret;
private Transform bulletSpawnPos;
private float shootRate = 3f;
private float elapsedTime;
private bool isDead;
private GameObject player;
protected override void Initialize()
{
wandarPoints = GameObject.FindGameObjectsWithTag("WandarPoint");
player = GameObject.FindGameObjectWithTag("Player");
playerTransform =player.transform;
turret = enemyTankTurret.transform;
bulletSpawnPos = enemyTankBulletSpawnPos;
CurrentHealth = health;
playerContoller = player.GetComponent<PlayerContoller>();
FindNextDestination();
}
private void FindNextDestination()
{
int randomIndex = Random.Range(0, wandarPoints.Length);
destinationPos = wandarPoints[randomIndex].transform.position;
}
protected override void FSMUpdate()
{
switch (currentState)
{
case FSMStates.Patrol : StatePatrol(); break;
case FSMStates.Chase : StateChase(); break;
case FSMStates.Attack : StateAttack(); break;
case FSMStates.Dead : StateDead(); break;
}
elapsedTime += Time.deltaTime;
if (CurrentHealth <= 0)
{
currentState = FSMStates.Dead;
}
}
private void StatePatrol()
{
if (Vector3.Distance(transform.position, destinationPos) <= 5f)
{
FindNextDestination();
}
else if (Vector3.Distance(transform.position, playerTransform.position) <= 40f)
{
currentState = FSMStates.Chase;
}
MoveAndRotateTowardsDestination();
}
private void StateChase()
{
destinationPos = playerTransform.position;
float distanceToAttack = Vector3.Distance(transform.position, playerTransform.position);
if (distanceToAttack <= 30f)
{
currentState = FSMStates.Attack;
}
else if (distanceToAttack >= 40f)
{
currentState = FSMStates.Patrol;
}
MoveAndRotateTowardsDestination();
}
private void StateAttack()
{
destinationPos = playerTransform.position;
float distanceToAttack = Vector3.Distance(transform.position, playerTransform.position);
if (distanceToAttack < 20f)
{
MoveAndRotateTowardsDestination();
currentState = FSMStates.Attack;
}
else if (distanceToAttack >= 20f)
{
currentState = FSMStates.Patrol;
}
Vector3 f = Vector3.Normalize(playerTransform.forward);// normalized(playerTransform.forward); //playerTransform.forward.
int speed = playerContoller.IsStop == true ? 0 :2;
Quaternion newRotation = Quaternion.LookRotation(playerTransform.position+f*speed - transform.position);
turret.rotation = Quaternion.Slerp(turret.rotation, newRotation, Time.deltaTime * 10f);
ShootBullet();
}
private void ShootBullet()
{
if (elapsedTime >= shootRate)
{
Instantiate(bulletPrefab, bulletSpawnPos.position, bulletSpawnPos.rotation);
elapsedTime = 0f;
}
}
private void MoveAndRotateTowardsDestination()
{
Quaternion targetRotation = Quaternion.LookRotation(destinationPos - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 10f);
transform.Translate(Vector3.forward * 3f * Time.deltaTime);
}
public void ReceiveDamage(int damage)
{
CurrentHealth -= damage;
}
private void StateDead()
{
if (!isDead)
{
isDead = true;
Destroy(gameObject);
}
}
}
边栏推荐
- Redis 排查大 key 的4种方法,优化必备
- 中国溶聚丁苯橡胶(SSBR)行业研究与预测报告(2022版)
- Rip notes [rip three timers, the role of horizontal segmentation, rip automatic summary, and the role of network]
- Function template
- xss注入
- AutoCAD - graphic input and output
- SQLServer 存储过程传递数组参数
- Personal required code
- 3dsmax2018 common operations and some shortcut keys of editable polygons
- 質量體系建設之路的分分合合
猜你喜欢
Number theoretic function and its summation to be updated
LeetCode之單詞搜索(回溯法求解)
Flutter tips: various fancy nesting of listview and pageview
用 Jmeter 工具做个小型压力测试
669. 修剪二叉搜索树 ●●
Unity get component
C4D simple cloth (version above R21)
2022 U.S. college students' mathematical modeling e problem ideas / 2022 U.S. game e problem analysis
Séparation et combinaison de la construction du système qualité
[Business Research Report] top ten trends of science and technology and it in 2022 - with download link
随机推荐
[PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)
[goweb development] Introduction to authentication modes based on cookies, sessions and JWT tokens
Download the details and sequence of the original data access from the ENA database in EBI
[Business Research Report] Research Report on male consumption trends in other economic times -- with download link
The 22nd Spring Festival Gala, an immersive stage for the yuan universe to shine into reality
中国金刚烷行业研究与投资预测报告(2022版)
3dsmax snaps to frozen objects
mysql審計日志歸檔
Out and ref functions of unity
Use assimp library to read MTL file data
AutoCAD - continuous annotation
AutoCAD - graphic input and output
English topic assignment (27)
[AI bulletin 20220211] the hard core up owner has built a lidar and detailed AI accelerator
2020-10-27
775 Div.1 B. integral array mathematics
#775 Div.1 B. Integral Array 数学
Manually implement heap sorting -838 Heap sort
Personal required code
JVM 原理和流程简介