我的问题是三个敌人同时射击。我要第一个然后第二个然后第三个开始射击。
这是我的代码:
public float speed = 7f;
public float attackDelay = 3f;
public Projectile projectile;
private Animator animator;
public AudioClip attackSound;
void Start () {
animator = GetComponent<Animator> ();
if(attackDelay > 0){
StartCoroutine(onAttack());
}
}
void Update () {
animator.SetInteger("AnimState", 0);
}
IEnumerator onAttack(){
yield return new WaitForSeconds(attackDelay);
fire();
StartCoroutine(onAttack());
}
void fire(){
animator.SetInteger("AnimState", 1);
if(attackSound){
AudioSource.PlayClipAtPoint(attackSound, transform.position);
}
}
void onShoot(){
if (projectile){
Projectile clone = Instantiate(projectile, transform.position, Quaternion.identity)as Projectile;
if(transform.localEulerAngles.z == 0){
clone.rigidbody2D.velocity = new Vector2(0, transform.localScale.y) * speed * -1;
}
else if(Mathf.RoundToInt(transform.localEulerAngles.z) == 90){
clone.rigidbody2D.velocity = new Vector2 (transform.localScale.x, 0) * speed;
}
else if(Mathf.RoundToInt(transform.localEulerAngles.z) == 180){
clone.rigidbody2D.velocity = new Vector2 (0, transform.localScale.y) * speed;
}
else if(Mathf.RoundToInt(transform.localEulerAngles.z) == 270){
clone.rigidbody2D.velocity = new Vector2(transform.localScale.x, 0) * speed * -1;
}
}
onShoot() 方法在动画中作为事件调用。
你们对此有什么建议吗?
好吧,一种方法(虽然可能不是最好的)是在 Start()
中添加延迟。你可以有这样的东西:
public float startDelay;
...
void Start()
{
...
StartCoroutine(startDelay());
}
IEnumerator startDelay()
{
yield return new WaitForSeconds(startDelay);
StartCoroutine(onAttack());
}
有了它,您只需按照您认为合适的方式设置 startDelay
。因为它是一个公共(public)变量,所以您可以在各个游戏对象的检查器中设置它(如果您在脚本中设置它,每个对象可能会有相同的延迟,没有区别)。
另一种方法可能是随机化 attackDelay
。使用 Random.Range
确定 attackDelay
,同时将其保持在合理范围内。
我想您可能还想重新考虑一下敌人的运作方式。也许让他们在玩家越过触发器时射击,或者加入一些逻辑让他们在玩家进入范围内时射击。
我是一名优秀的程序员,十分优秀!