当前位置:网站首页>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);
        }
    }
}

原网站

版权声明
本文为[yoyoHm]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140626088053.html