irpg Community
สคริปต์ ai - Printable Version

+- irpg Community (http://irpg.in.th)
+-- Forum: Publica Foundation (http://irpg.in.th/forum-23.html)
+--- Forum: Request Ranger Center (http://irpg.in.th/forum-24.html)
+--- Thread: สคริปต์ ai (/thread-1429.html)



สคริปต์ ai - lightwolfz - 09-27-2013

ต้องการสคริปต์ 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


RE: สคริปต์ ai - นิราจ - 09-27-2013

สุดยอดเลยครับ
โค้ดกระชับดีครับ


RE: สคริปต์ ai - Author - 09-27-2013

ถ้าต้องการ ทำดาเมจ (ลดตัวแปร curHealth) จากสคริปตัวละคร

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

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

เอกสาร ScriptReference ช่วยได้เยอะครับ เวลาคิดไม่ออก ลองอ่านศึกษาแล้วลองเขียนเองครับ ไม่ยาก ; )


RE: สคริปต์ ai - Griever - 09-27-2013

๛ส่วนเรื่องการชนแล้วอยากให้ลดดาเมจนั้น ให้ใช้ OnCollisionEnter ดูครับ
แล้วเรื่องการลดตัวแปรเลือดก็ตามที่ตา Author บอกไว้

เช่น


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




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

ปล. จะลดเลือดเท่าไหร่นั้นตั้งเป็นตัวแปรเอาก็ได้ครับ จะได้ยืดหยุ่นกว่า