当前位置:网站首页>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);
}
}
}
边栏推荐
- Unity parallax infinite scrolling background
- AutoCAD - lengthening
- Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
- Out and ref functions of unity
- Wan broadband access technology V EPON Technology
- 【acwing】837. Number of connected block points
- mysql審計日志歸檔
- An article takes you to thoroughly understand descriptors
- Establish cloth effect in 10 seconds
- Interface joint commissioning test script optimization V5.0 (end)
猜你喜欢
[groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)
2021 huashubei mathematical modeling idea + reference + paper
Unity parallax infinite scrolling background
PostgreSQL surpasses mysql, and the salary of "the best programming language in the world" is low
LeetCode之單詞搜索(回溯法求解)
CUDA Programming atomic operation atomicadd reports error err:msb3721, return code 1
2022 thinking of mathematical modeling D problem of American college students / analysis of 2022 American competition D problem
Unity get component
AutoCAD - workspace settings
669. Prune binary search tree ●●
随机推荐
Common database statements in unity
MySQL audit log Archive
China needle coke industry development research and investment value report (2022 Edition)
[PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)
中国聚氨酯硬泡市场调研与投资预测报告(2022版)
3dsmax common commands
2022 thinking of mathematical modeling a problem of American college students / analysis of 2022 American competition a problem
China polyurethane rigid foam Market Research and investment forecast report (2022 Edition)
AutoCAD - stretching
Thinking of 2022 American College Students' mathematical modeling competition
Unity3d learning notes
Sqlserver stored procedures pass array parameters
質量體系建設之路的分分合合
次小生成树
【acwing】836. Merge sets
Download the details and sequence of the original data access from the ENA database in EBI
Special information | finance, accounting, audit - 22.1.23
MySQL in-depth learning - index creation and deletion, index design principles, index failure scenarios, query optimization, index push down ICP
Database under unity
"Measuring curve length" of CAD dream drawing