简体   繁体   English

Unity3d - 当玩家静止时炮塔停止射击

[英]Unity3d - Turret stops shooting when player is stationary

I'm currently developing a 2D top down game and I have a problem with my Turret AI.我目前正在开发一款 2D 自上而下的游戏,但我的炮塔 AI 有问题。 Everything works perfectly except, when the player is stationary, the turret stops shooting.一切正常,除了当玩家静止时,炮塔停止射击。 The bulletTimer stops increasing when player stops, instead of increasing over time.当玩家停止时,bulletTimer 停止增加,而不是随着时间的推移而增加。 I'm guessing it has something to do with Time.deltaTime and frame refreshing but couldn't find a workaround.我猜它与 Time.deltaTime 和帧刷新有关,但找不到解决方法。 Do I need to write an attack function with IEnumerator and waitforseconds?我是否需要使用 IEnumerator 和 waitforseconds 编写攻击函数? It feels like it's a simple problem yet I'm unable to solve it.感觉这是一个简单的问题,但我无法解决它。 Thanks for the help.谢谢您的帮助。 Here is my code :这是我的代码:

TurretAI code炮塔AI代码

using UnityEngine;
using System.Collections;

public class TurretAI : MonoBehaviour 
{ 
     public float FireRate;
     public float bulletSpeed;
     public float bulletTimer;

     public GameObject Gun;
     public GameObject bullet;
     public Transform target;


     public void Attack()
     {    
         bulletTimer += Time.deltaTime;

         if (bulletTimer >= FireRate) 
         {
             Vector2 direction = target.transform.position - transform.position;
             direction.Normalize ();

             GameObject bulletClone;
             bulletClone = Instantiate (bullet, Gun.transform.position, Gun.transform.rotation) as GameObject;
             bulletClone.GetComponent<Rigidbody2D> ().velocity = direction * bulletSpeed;

             bulletTimer = 0;
         }
     }
 }

And this is the Turret Trigger code这是炮塔触发代码

using UnityEngine;
using System.Collections;

public class TurretTrigger : MonoBehaviour 
{ 
    public TurretAI turretAI;

    void Awake()
    { 
        turretAI = gameObject.GetComponentInParent<TurretAI> (); 
    }

     void OnTriggerStay2D(Collider2D Trigger)
     {
         if (Trigger.CompareTag ("Player")) 
         {
             turretAI.Attack ();
         }
     }
}

I think that your player's rigidBody2D is falling asleep when it's not moving (this is the engine optimizing performance, nothing that you have done).我认为您的播放器的rigidBody2D在不动时正在入睡(这是引擎优化性能,您没有做任何事情)。 When rigidBody s sleep, the OnTriggerStay2D will stop detecting them.rigidBody的睡眠中, OnTriggerStay2D将停止检测它们。 Calling rigidBody2D.WakeUp() on your player's rigidBody2D will force the rigidBody to wake up and be detected by the trigger box.在玩家的rigidBody2D上调用rigidBody2D.WakeUp()将强制rigidBody唤醒并被触发框检测到。

I can't find a more efficient way of making sure that the rigidBody stays awake just now.我找不到更有效的方法来确保rigidBody现在保持清醒。 Try this to see if it what is causing your problem and then, if it is, try to find a more efficient way of stooping the ridgidBody sleeping!试试这个,看看是否是什么导致了你的问题,然后,如果是,尝试找到一种更有效的方法来让ridgidBody睡觉!

Edit: Gunnar B has pointed out that this can be set in the inspector.编辑:Gunnar B 指出这可以在检查器中设置。 Go to your player's RigidBody` component and set 'Sleeping Mode' to 'Never Sleep'.转到播放器的 RigidBody` 组件并将“睡眠模式”设置为“从不睡眠”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM