当前位置:网站首页>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);
}
}
}
边栏推荐
- Mode in BST (binary tree & Notes on question brushing)
- 【Leetcode】1352. Product of the last K numbers
- Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
- 2022 American College Students' mathematical modeling ABCDEF problem thinking /2022 American match ABCDEF problem analysis
- Redis 排查大 key 的4种方法,优化必备
- Debug insights
- 【Leetcode】1352. 最后 K 个数的乘积
- The 22nd Spring Festival Gala, an immersive stage for the yuan universe to shine into reality
- Error statuslogger log4j2 could not find a logging implementation
- Personal required code
猜你喜欢

The principle of attention mechanism and its application in seq2seq (bahadanau attention)

XSS injection

669. 修剪二叉搜索树 ●●

Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套

AutoCAD - workspace settings

C4D simple cloth (version above R21)

【acwing】528. cheese
![[groovy] closure (Introduction to closure class closure | this, owner, delegate member assignment and source code analysis)](/img/aa/3c8b7b27e322417777d1315b9a5a8f.jpg)
[groovy] closure (Introduction to closure class closure | this, owner, delegate member assignment and source code analysis)

AutoCAD - isometric annotation

Create a pyGame window with a blue background
随机推荐
AutoCAD - stretching
GameObject class and transform class of unity
2022 thinking of mathematical modeling a problem of American college students / analysis of 2022 American competition a problem
Function template
Special information | finance, accounting, audit - 22.1.23
中国金刚烷行业研究与投资预测报告(2022版)
Flink cluster configuration
Common database statements in unity
JMeter -- distributed pressure measurement
flutter 对象和列表
猿人学第一题
Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
Rip notes [rip three timers, the role of horizontal segmentation, rip automatic summary, and the role of network]
[groovy] closure (Introduction to closure class closure | closure parametertypes and maximumnumberofparameters member usage)
【Leetcode】1352. 最后 K 个数的乘积
An article takes you to thoroughly understand descriptors
AutoCAD -- dimension break
Forecast report on research and investment prospects of Chinese wormwood industry (2022 Edition)
54. 螺旋矩阵 & 59. 螺旋矩阵 II ●●
This article is good