[Hướng Dẫn] Làm AI đơn giản
[align=center]
[/align]
Từ bài http://taotrochoi.com/showthread.php?tid=2055 chúng ta sẽ tạo ra một AI đơn giản có thể rượt theo player. Một tính năng cơ bản trong thuật toán tìm đường (path finding).
B1. Nhấp chuột phải vào thư mục Script và chọn Create | C# Script, đặt tên là EnemyAI và nhập đoạn code sau vào:

B2. Kéo thả file C# vừa tạo vào đối tượng Evil Cube trong thẻ Hierarchy.

B3. Tại thẻ Hierarchy, nhấp chọn Player, qua thẻ Inspector, đặt tag thành Player.

B4. Bấm Ctrl + S để save Scene của bạn lại để có thể tiếp tục thực hiện tiếp các bài hướng dẫn sau.

B5. Ấn Play để kiểm tra kết quả.

Nguồn : http://vietunity3d.blogspot.com/

Từ bài http://taotrochoi.com/showthread.php?tid=2055 chúng ta sẽ tạo ra một AI đơn giản có thể rượt theo player. Một tính năng cơ bản trong thuật toán tìm đường (path finding).
B1. Nhấp chuột phải vào thư mục Script và chọn Create | C# Script, đặt tên là EnemyAI và nhập đoạn code sau vào:

using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
private Transform myTransform;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
moveSpeed = 5;
rotationSpeed = 5;
target = go.transform;
}
// Update is called once per frame
void Update () {
//Debug.DrawLine(target.transform.position, myTransform.position, Color.yellow);
Debug.DrawLine(target.position, myTransform.position, Color.yellow);
//Look at the target
//myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position, //myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
// Move towards target
//myTransform.position += Vector3.forward * moveSpeed * Time.deltaTime;
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
B2. Kéo thả file C# vừa tạo vào đối tượng Evil Cube trong thẻ Hierarchy.

B3. Tại thẻ Hierarchy, nhấp chọn Player, qua thẻ Inspector, đặt tag thành Player.

B4. Bấm Ctrl + S để save Scene của bạn lại để có thể tiếp tục thực hiện tiếp các bài hướng dẫn sau.

B5. Ấn Play để kiểm tra kết quả.

Nguồn : http://vietunity3d.blogspot.com/