unity 場景NPC觸發漫游和間隔時間自動顯示對話在人物上方的簡單腳本!


using UnityEngine;
using System.Collections;

public class NPC : MonoBehaviour {

public GameObject ying_1;
private float shi_1 = 0;
//private float fireRate = 10F;

void Update()
{
//隱藏物體
ying_1.SetActiveRecursively(false);
// 向前以每秒2米的移動
transform.Translate(Vector3.forward * Time.deltaTime*2);
gameObject.animation.Play("walk");
//時間變化
//並且 每隔10做一次事件,在15秒的時候時間從0開始運行!
shi_1+=Time.deltaTime;
//10秒的時候做一次事件
if(shi_1>=10.0f)
{

//顯示物體
ying_1.SetActiveRecursively(true);

}
//15秒的做次事件

if(shi_1>=15.0f)
{
//時間歸0
shi_1-=15.0f;
ying_1.SetActiveRecursively(false);

}

}

//碰撞觸發
void OnTriggerEnter(Collider other) {

//4個方向隨即旋轉角度120°
Quaternion rotate = Quaternion.Euler(0,Random.Range(1,5) *120,0);
transform.rotation = Quaternion.Slerp(transform.rotation,rotate, Time.deltaTime*100);
//播放動畫
gameObject.animation.Play("walk");




}

}


 

using UnityEngine;
using System.Collections;

public class NPC_1: MonoBehaviour {

public GameObject ying_1;
private float shi_1 = 0;
RaycastHit hit;

void Update()
{
transform.Translate(Vector3.forward * Time.deltaTime*2);
gameObject.animation.Play("walk");
if (Physics.Raycast(transform.position, Vector3.forward, out hit, 5F))
{
if(hit.collider.gameObject.tag=="cube1")
{
Quaternion rotate = Quaternion.Euler(0,Random.Range(1,5) *120,0);
transform.rotation = Quaternion.Slerp(transform.rotation,rotate, Time.deltaTime*100);
gameObject.animation.Play("walk");
}


}
shi_1+=Time.deltaTime;

ying_1.SetActiveRecursively(false);
if(shi_1>=10.0f)
{
ying_1.SetActiveRecursively(true);


}

if(shi_1>=15.0f)
{
shi_1-=15.0f;
ying_1.SetActiveRecursively(false);

}
}
void Start()
{



}



注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com