当前位置:网站首页>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);
}
}
}
边栏推荐
- Sixth note
- PR first time
- [groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)
- 2021 electrician cup (the 12th "China Society of electrical engineering Cup" National Undergraduate electrician mathematical modeling) detailed ideas + codes + references
- CUDA Programming atomic operation atomicadd reports error err:msb3721, return code 1
- Understand encodefloatrgba and decodefloatrgba
- Unity parallax infinite scrolling background
- Introduction to JVM principle and process
- 2021 electrician cup idea + code - photovoltaic building integration plate index development trend analysis and prediction: prediction planning issues
- #775 Div.1 B. Integral Array 数学
猜你喜欢
Create a pyGame window with a blue background
[Business Research Report] top ten trends of science and technology and it in 2022 - with download link
AutoCAD - full screen display
Rip notes [rip message security authentication, increase of rip interface measurement]
Autocad-- Real Time zoom
AutoCAD - Document Management
AutoCAD - window zoom
介绍汉明距离及计算示例
Flutter tips: various fancy nesting of listview and pageview
An article takes you to thoroughly understand descriptors
随机推荐
Download the details and sequence of the original data access from the ENA database in EBI
2021 huashubei mathematical modeling idea + reference + paper
2021 electrician cup (the 12th "China Society of electrical engineering Cup" National Undergraduate electrician mathematical modeling) detailed ideas + codes + references
中国溶聚丁苯橡胶(SSBR)行业研究与预测报告(2022版)
【acwing】837. Number of connected block points
775 Div.1 C. Tyler and strings combinatorial mathematics
3dsmax common commands
[groovy] closure (closure call | closure default parameter it | code example)
LeetCode之單詞搜索(回溯法求解)
AutoCAD - stretching
Common technologies of unity
AutoCAD - Zoom previous
49 pictures and 26 questions explain in detail what is WiFi?
PostgreSQL surpasses mysql, and the salary of "the best programming language in the world" is low
[groovy] closure (closure parameter binding | curry function | rcurry function | ncurry function | code example)
[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)
Pdf to DWG in CAD
Establish cloth effect in 10 seconds
Detailed introduction of OSPF header message