使用 c# 统一每 3 秒延迟一次拍摄
delay on the shot every 3 seconds in unity using c#
抱歉,我是 c# 和 unity 的新手...我想要的是执行 spawner.SendMessage("shoot");每3秒..它只会在满足条件时执行。
void Update(){
//first we draw a line from our enemy to our player
Debug.DrawLine (player.position, myTransform.position, Color.red);
playerDistance = Vector3.Distance(player.position, myTransform.position);
if (playerDistance < 40f) {
lookAtPlayer ();
}
if(playerDistance <= 38f){
if(playerDistance > 21){
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
spawner.SendMessage("shoot");
}
}
}
我说清楚,这是给NPC类型的对象吗?您要构建 Enemy AI 吗?
如果你只是想让他在向目标移动时每 3 秒射击一次,那么以下代码就足够了:
float timer;
void Start()
{
timer = 0;
}
void Update()
{
timer += Time.deltaTime;
if (playerDistance <= 38 && playerDistance > 21)
{
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
if (timer > 3.0f)
{
spawner.SendMessage("shoot");
timer = 0;
}
}
}
抱歉,我是 c# 和 unity 的新手...我想要的是执行 spawner.SendMessage("shoot");每3秒..它只会在满足条件时执行。
void Update(){
//first we draw a line from our enemy to our player
Debug.DrawLine (player.position, myTransform.position, Color.red);
playerDistance = Vector3.Distance(player.position, myTransform.position);
if (playerDistance < 40f) {
lookAtPlayer ();
}
if(playerDistance <= 38f){
if(playerDistance > 21){
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
spawner.SendMessage("shoot");
}
}
}
我说清楚,这是给NPC类型的对象吗?您要构建 Enemy AI 吗?
如果你只是想让他在向目标移动时每 3 秒射击一次,那么以下代码就足够了:
float timer;
void Start()
{
timer = 0;
}
void Update()
{
timer += Time.deltaTime;
if (playerDistance <= 38 && playerDistance > 21)
{
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
if (timer > 3.0f)
{
spawner.SendMessage("shoot");
timer = 0;
}
}
}