Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unity สคริปต์ ai
#1
Question 
ต้องการสคริปต์ ai ของ unity ครับ ตามหัวข้อเบย กะจะเอาไปใส่เกมแต่หาในเน็ตมีแต่อันที่ผมใช้ไม่ได้ T^T

ขอเป็นสคริปต์ของเกม fps นะครับ แล้วก็ขอ script เลือดของตัวละครเราด้วย ตอนนี้ผมมีแค่สคริปต์ ai เดินมาหาเราและพุ่งชนเรา Oh My God แต่ไม่ลด health ก็เลยอยากได้ ai ชนเราแล้วมี damage ด้วย

อันนี้สคริปต์ที่ผมใช้ครับ

สคริปต์ ai
Quote:var Distance;
var Target : Transform;
var lookAtDistance = 25.0;
var chaseRange = 15.0;
var attackRange = 1.5;
var moveSpeed = 5.0;
var Damping = 6.0;
var attackRepeatTime = 1;

var TheDammage = 40;

private var attackTime : float;

var controller : CharacterController;
var gravity : float = 20.0;
private var MoveDirection : Vector3 = Vector3.zero;

function Start ()
{
attackTime = Time.time;
}

function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);

if (Distance < lookAtDistance)
{
lookAt();
}

if (Distance > lookAtDistance)
{
renderer.material.color = Color.green;
}

if (Distance < attackRange)
{
attack();
}
else if (Distance < chaseRange)
{
chase ();
}
}

function lookAt ()
{
renderer.material.color = Color.yellow;
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}

function chase ()
{
renderer.material.color = Color.red;

moveDirection = transform.forward;
moveDirection *= moveSpeed;

moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}

function attack ()
{
if (Time.time > attackTime)
{
Target.SendMessage("ApplyDammage", TheDammage);
Debug.Log("The Enemy Has Attacked");
attackTime = Time.time + attackRepeatTime;
}
}

function ApplyDammage ()
{
chaseRange += 30;
moveSpeed += 2;
lookAtDistance += 40;
}

ส่วนอันนี้สคริปต์health ของตัวละครเรา

Quote:var curHealth : int = 100;
var maxHealth : int = 100;

var healthtext : GUIText;



function Start () {


healthRegen();

}





function Update () {


healthtext.text = curHealth + " / " + maxHealth;


if(curHealth < 0 ) {

curHealth = 0;


}


if(curHealth > 100) {


curHealth = 100;


}



if(Input.GetKeyDown("e")) {

curHealth -= 10;

}



}




function healthRegen () {


for(i=1;i>0;i++) {



yield WaitForSeconds(0.5);

if(curHealth < maxHealth) {

curHealth++;

}


}


}

ต้องการทำให้สัมพันธ์กัน /chan

ขอบคุณล่วงหน้าเลยด้วยนะครับ Derp
Y me gusta muchoY me gusta muchoY me gusta muchoY me gusta muchoY me gusta muchoY me gusta mucho
[-] The following 1 user says Thank You to lightwolfz for this post:
  • นิราจ
Reply
#2
สุดยอดเลยครับ
โค้ดกระชับดีครับ
[Image: btpni.jpg]
[-] The following 1 user says Thank You to นิราจ for this post:
  • lightwolfz
Reply
#3
ถ้าต้องการ ทำดาเมจ (ลดตัวแปร curHealth) จากสคริปตัวละคร

ใช้คำสั่ง Getcomponent ครับ
อ้างอิง : http://docs.unity3d.com/Documentation/Sc...onent.html

มันสามารถเข้าถึงตัวแปรในสคริปอื่นได้ Wink

เอกสาร ScriptReference ช่วยได้เยอะครับ เวลาคิดไม่ออก ลองอ่านศึกษาแล้วลองเขียนเองครับ ไม่ยาก ; )
[-] The following 1 user says Thank You to Author for this post:
  • lightwolfz
Reply
#4
๛ส่วนเรื่องการชนแล้วอยากให้ลดดาเมจนั้น ให้ใช้ OnCollisionEnter ดูครับ
แล้วเรื่องการลดตัวแปรเลือดก็ตามที่ตา Author บอกไว้

เช่น


Code:
function OnCollisionEnter(col : Collision) {
    if (col.tag == "Player"){
         col.GetComponent(ชื่อสคริปเลือด).curHealth -= 10;
    }
}




เอาไปใส่ไว้ในสคริป AI เมื่อมันไปชนกับ Player ก็จะลดเลือด 10(อย่าลืมเซต tag ตัวผู้เล่น
เป็น Player ด้วย)

ปล. จะลดเลือดเท่าไหร่นั้นตั้งเป็นตัวแปรเอาก็ได้ครับ จะได้ยืดหยุ่นกว่า
[-] The following 1 user says Thank You to Griever for this post:
  • lightwolfz
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)