I have the following C# code:
using UnityEngine;
using System.Collections;
public class Hero : MonoBehaviour {
float speed = 2f;
bool hero_up = false;
bool hero_down = false;
bool hero_left = false;
bool hero_right = false;
public Animator animator;
public Rigidbody2D rbEnemy;
// Use this for initialization
void Start ()
{
animator = GetComponent<Animator> ();
EnemySpawn ();
}
void EnemySpawn()
{
Rigidbody2D EnemyInstance;
EnemyInstance = Instantiate(rbEnemy, new Vector3(Random.Range (2f, 8f), Random.Range (-4f, 4f) ,0f), Quaternion.Euler(new Vector3(0f,0f,0f))) as Rigidbody2D;
}
// Update is called once per frame
InvokeRepeating("EnemySpawn", 3, 3);
}
I receive the following message error:
error CS1519: Unexpected symbol `EnemySpawn' in class, struct, or interface member declaration
Public variables (Animator and Rigidbody2d) are correctly set
Where should move InvokeRepeating ? I searched some answers; I moved InvokeRepeating in Start and at the the end of the EnemySpawn. The results was that an increasing number of enemy for each frame. What is the solution of this problem ?
答案 0 :(得分:1)
using UnityEngine;
using System.Collections;
public class Hero : MonoBehaviour {
float speed = 2f;
bool hero_up = false;
bool hero_down = false;
bool hero_left = false;
bool hero_right = false;
public Animator animator;
public Rigidbody2D rbEnemy;
// Use this for initialization
void Start ()
{
animator = GetComponent<Animator> ();
// Invokes the method methodName in time seconds, then repeatedly every repeatRate seconds.
InvokeRepeating("EnemySpawn", 3, 3);
}
void EnemySpawn()
{
Rigidbody2D EnemyInstance;
EnemyInstance = Instantiate(rbEnemy, new Vector3(Random.Range (2f, 8f), Random.Range (-4f, 4f) ,0f), Quaternion.Euler(new Vector3(0f,0f,0f))) as Rigidbody2D;
}
}
正如你所说的那样增加敌人的生成,然后使用大的repeRate,即InvokeRepeating方法的第三个参数。
如果你做InvokeRepeating(“Function”,1.0f,1.0f),它会在InvokeRepeating调用后一秒调用Function,然后每隔一秒调用一次