Thứ Hai, 17 tháng 4, 2023

ENEMY AI - trong Unity

 video hướng  dẫn :EnemyAI và code



EnemyStart.c#

using System.Collections

using System.Collections.Generic;

using UnityEngine;


//video youtube : ENEMY AI - Making an RPG in Unity (E10)

// trong object Enemy co NavMeshAgent, capsulecollider

 //va Rigidbody 

// use gravity and is Kinematic in Rigidbody

//script EnemyController.c#, Enemy.c#, EnemyStart.c#


public class EnemyStarts : CharacterStarts

{

// phong them vao  video co public MaxHeatlth

public float  MaxHealth = 40;

public override void Die()

{

base Die();

//Add ragdoll effect /Death animation

Destroy(GameObject);

}

}

-----------------------------------------------

Enemy.c#
using System.Collections

using System.Collections.Generic;

using UnityEngine;


//video youtube : ENEMY AI - Making an RPG in Unity (E10)

// trong object Enemy co NavMeshAgent, capsulecollider

 //va Rigidbody 

// use gravity and is Kinematic in Rigidbody

//script EnemyController.c#, Enemy.c#, EnemyStart.c#


public class Enemy : MonoBehaviour

{

public override void Interract()

{

base.Interract();

}

}

---------------------------------------'

EnemyController.c#

using System.Collections

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.AI;

//video youtube : ENEMY AI - Making an RPG in Unity (E10)

// trong object Enemy co NavMeshAgent, capsulecollider

 //va Rigidbody voi thuoc tinh va script EnemyController

// use gravity and is Kinematic


public class EnemyController : MonoBehaviour

{

public float lookRadius;

Transform target;

NavMeshAgent agent;

void Start()

{

   Target = PlayerManager.instance.player.transform;  // sinh ra player khi chayj chuong trinh do vay ta co the  update

   agent = GetComponent<NavMeshAgent>();

}

void Update()

{

float distance = Vector3.Distance(target.position, transform.position);

if(distance <= lookRadius)

{

agent.SetDestination(target.position);

if(distance <= agent.stoppingDistance)

{

// attack the target

FaceTarget(); // face to the target

}

}

}

voidFaceTarget()

{

Vector3 direction = (target.position - transform.position).normalized;

Quaternion lookRotation = Quaternion.LookRotation(new Vaector3(direction.x, 0, direction.y));

transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f); // noi suy hinh cau de xoay cho muot hon

}

}

-------------------

PlayerManager.c#

using System.Collections;

using System.Collections.Genenric;

System.Collections.Generic;

using UnityEngine;

// GameManager la doi tuong duoc tao trong cua so Hierachy

// video youtube ENEMY AI - Making an RPG in Unity (E10)

//gameobject GameManager bao gom cac component sau script :Inventory.c#, 

// EquypmentManager.c#, PlayerManager.c#

public class PlayerManager : MonoBehaviour 

{

   #region Singleton

public static PlayerManager instance;


void Awake()

{

instance = this;

}

public GameObject player;

  #endregion

void Start()

{

   agent = GetComponent<NavMeshAgent>();

}

}


0 nhận xét: