Я очень новичок в единстве, и у меня были проблемы с моим экземпляром готового сборного дома.Я пытаюсь заставить свой экземплярный префаб двигаться, как только он загружается на сцену, однако проблема в том, что он вообще не двигается.Объект загружается в мою сцену, но остается статичным.Я попытался добавить в Update () и FixedUpdate () и переместить вражеского Move () в них, но он все еще не работает.Я не уверен, в чем может быть проблема.
void Awake()
{
rigidbody2DComponent = enemyPrefab.GetComponent<Rigidbody2D>();
initialYPosition = transform.position.y;
}
void Start()
{
enemyObject = Instantiate(enemyPrefab, enemyInitialPosition.position, transform.rotation);
enemyObject.name = "Enemy";
enemyObject.transform.parent = transform;
enemyMove();
}
void enemyMove()
{
speed = Random.Range(-10, -20);
rigidbody2DComponent.AddForce(transform.up * speed, ForceMode2D.Force);
//keep track of the old x position
initialXPosition = transform.position.x;
//store the new x position
newXPosition = initialXPosition;
//new x position cannot be the same as the old x position
while (Mathf.Abs(newXPosition - initialXPosition) < 1)
{
newXPosition = Random.Range(-6f, 6f);
}
}
public void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player" || other.tag == "resetWall")
{
enemyMove();
//instantiate a new enemy object everytime it hits player or the bottom wall
newEnemyObject = (GameObject) GameObject.Instantiate(enemyObject, new Vector2(newXPosition, initialYPosition), transform.rotation);
//Without changing the name, the original name will get a bunch of
//(clone) added to it as it respawns
newEnemyObject.name = "newEnemy";
//Destroy the old enemy
Destroy(this.gameObject);
}
}`