当前位置:网站首页>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);
}
}
}
边栏推荐
- Autocad-- Real Time zoom
- The 22nd Spring Festival Gala, an immersive stage for the yuan universe to shine into reality
- JVM 原理和流程简介
- C iterator
- Special information | real estate and office buildings - 22.1.9
- Error statuslogger log4j2 could not find a logging implementation
- 2021 huashubei mathematical modeling idea + reference + paper
- [Chongqing Guangdong education] National Open University 2047t commercial bank operation and management reference test in autumn 2018
- Variable category (automatic, static, register, external)
- C4D simple cloth (version above R21)
猜你喜欢

2021 Higher Education Club Cup mathematical modeling national tournament ABCD problem - problem solving ideas

On-off and on-off of quality system construction

【acwing】836. Merge sets

Flutter tips: various fancy nesting of listview and pageview

LeetCode之單詞搜索(回溯法求解)

AutoCAD - set layer

54. Spiral matrix & 59 Spiral matrix II ●●

AutoCAD - window zoom

The 22nd Spring Festival Gala, an immersive stage for the yuan universe to shine into reality

【Leetcode】1352. Product of the last K numbers
随机推荐
Create a pyGame window with a blue background
[groovy] closure (closure parameter binding | curry function | rcurry function | ncurry function | code example)
猿人学第一题
mysql审计日志归档
How to choose a panoramic camera that suits you?
PostgreSQL 超越 MySQL,“世界上最好的编程语言”薪水偏低
PostgreSQL surpasses mysql, and the salary of "the best programming language in the world" is low
flutter 对象和列表
Research and investment forecast report of adamantane industry in China (2022 Edition)
2022 U.S. college students' mathematical modeling e problem ideas / 2022 U.S. game e problem analysis
English topic assignment (27)
2021-10-29
2022 thinking of Mathematical Modeling B problem of American college students / analysis of 2022 American competition B problem
【acwing】836. Merge sets
MySQL audit log archiving
3dsmax common commands
2020-10-27
AutoCAD - set layer
Emlog博客主题模板源码简约好看响应式
質量體系建設之路的分分合合