[Unity3D] Simple AI

1. เตรียมโปรเจ็กส์
2. สร้าง Player
3. สร้าง Enemy
4. ใส่ Simple AI ให้ Enemy

1. เตรียมโปรเจ็กส์

สร้างโปรเจ็กส์

  • สร้างโปรเจ็กส์ชื่อ SimpleAI

สร้างโฟลเดอร์

  • สร้างโฟลเดอร์ชื่อ Scene
  • สร้างโฟลเดอร์ชื่อ Material
  • สร้างโฟลเดอร์ชื่อ Script

สร้าง Scene

  • ที่โฟลเดอร์ Scene ให้สร้าง Scene ขึ้นมาชื่อ MainScene

 

ที่ MainScene ให้สร้าง Plane1

  • Create > 3D Object > Plane
  • Rename to Plane1
  • Reset Transform
  • Scale = 2, 1, 2
  • ที่ โฟลเดอร์ Material สร้าง Material ชื่อ Plane1
  • ที่ Material > Plane1 กำหนดให้ Color = 40, 130, 120
  • ผูก Material > Plane1 กับ Plane1

ที่ Main Camera ให้ปรับมุมกล้อง

  • ที่ Main Camera ให้
  • Position = 0, 20, 0
  • Rotation = 90, 0, 0
  • Projection = Orthographic
  • Size = 10

 

2. สร้าง Player

ที่ MainScene ให้สร้าง Player

  • Create > 3D Object > Sphere
  • Rename to Player
  • Reset Transform
  • Position = 0, 0.5, 0
  • ใส่ Tag Player
  • ใส่ Rigidbody ให้ Player
  • Add Component > Physic > Rigidbody
  • ที่ โฟลเดอร์ Material สร้าง Material ชื่อ Player
  • ที่ Material > Player กำหนดให้ Color = 0, 140, 255
  • ผูก Material > Player กับ Player

ที่ Player

  • Add Component > New Script ชื่อ PlayerController
  • ลาก PlayerController ไปไว้ในโฟลเดอร์ Script
  • แก้ไขโค๊ด PlayerController

สคริป PlayerController

[code language=”csharp”]
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
public int speed = 10;

private Rigidbody rb;

void Start()
{
rb = GetComponent<Rigidbody>();
}

void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
//rb.AddForce(movement * speed);
rb.velocity = movement * speed;
}
}
[/code]

 

3. สร้าง Enemy

ที่ MainScene ให้สร้าง Enemy

  • Create > 3D Object > Sphere
  • Rename to Enemy
  • Reset Transform
  • Position = 0, 0.5, 8
  • ที่ Sphere Collider เลือก Is Trigger
  • ใส่ Rigidbody ให้ Enemy
  • Add Component > Physic > Rigidbody
  • ที่ โฟลเดอร์ Material สร้าง Material ชื่อ Enemy
  • ที่ Material > Enemy กำหนดให้ Color = 255, 40, 40
  • ผูก Material > Enemy กับ Enemy

ที่ Enemy

  • Add Component -> New Script ชื่อ DestroyByContact
  • ย้ำย DestroyByContact ไปไว้ในโฟลเดอร์ Script
  • แก้ไขโค๊ด DestroyByContact

สคริป DestroyByContact

[code language=”csharp”]
using UnityEngine;
using System.Collections;

public class DestroyByContact : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
Destroy(other.gameObject);
Destroy(gameObject);
}
}
}
[/code]

 

กำหนดพื้นที่ให้ Enemy เดิน (NAVMESH BAKING)

  • ไปที่เมนู Windows > AI > Navigation
  • ที่แท็บ Navigation เลือก Object
  • เลือก Plane1 แล้ว Checked ที่ Navigation Static
  • ที่แท็บ Navigation เลือก Bake จะเห็นค่า Default แต่เลือกปรับได้
    • Agent Radius = 0.5
    • Agent Height 2
    • Max Slope = 45
    • Step Height = 0.4
  •  กดปุ่ม Bake แล้วที่ Scene view จะเห็นพื้นที่สีฟ้า สำหรับให้ Agent (Enemy) เดิน

ที่ Enemy

  • Add Component > Navigation > Nav Mesh Agent
  • ค่า Default เลือกปรับได้
    • Radius = 0.5
    • Height = 1
    • Speed = 3.5
    • Stopping Distance = 0

4. ใส่ Simple AI ให้ Enemy

ที่ Enemy

  • Add Component > New Script ชื่อ EnemyController
  • ลาก EnemyController ไปไว้ในโฟลเดอร์ Script
  • แก้ไขโค๊ด EnemyController

สคริป EnemyController

[code language=”csharp”]using UnityEngine;
using UnityEngine;
using System.Collections;

public class EnemyController : MonoBehaviour
{
Transform player;
NavMeshAgent nav;

void Awake()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
nav = GetComponent<NavMeshAgent>();
}
void Update()
{
nav.SetDestination(player.position);
}
}
[/code]

 

  • เมื่อรันดู Enemy จะเดินตาม Player ละ

 

ดูเพ่ิ่มเติม