我对团结还很陌生,我的实例化预制件一直存在问题。我试图使实例化的预制件一旦加载到场景中即可移动,但是问题是它根本不移动。该对象加载到我的场景中,但保持静态。我尝试添加Update()和FixedUpdate()并将我的敌人移动()移入其中,但是它仍然不起作用。我不确定是什么问题。
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);
}
}`
答案 0 :(得分:0)
计算完下一个位置后,需要在对象上设置新位置。在您的敌人移动方法的末尾,添加它。
transform.position = new Vector3(newXPosition, newYPosition, transform.position.z);
或者如果不对Y部分进行计算,则将initialYPosition用作第二个参数。
答案 1 :(得分:0)
等等,您确定- hosts: win
gather_facts: no
tasks:
- name: Set port to 8888
xml:
path: C:\Program Files (x86)\Jenkins\jenkins.xml
xpath: /service/arguments/httpPort
value: 8888
的值小于7吗?如果没有,请放置Debug。登录
initialXPosition = transform.position.x;
也许您陷入了无限循环? while (Mathf.Abs(newXPosition - initialXPosition) < 1)
{
newXPosition = Random.Range(-6f, 6f);
Debug.Log("Infinity");
}
的值必须小于7,否则它永远不会退出此循环。
答案 2 :(得分:0)
对于那些可能遇到类似问题的人:
我需要做的实际上是将我的移动和触发功能与主脚本分离为一个附着在预制件上的脚本。
感谢ray2yar在统一答案论坛上帮助我解决问题!
这是主脚本的代码
void Start()
{
SpawnEnemy = this;
Spawn();
}
public void Spawn()
{
GameObject enemyObject = Instantiate(enemyPreFab, enemyInitialPosition.position, transform.rotation) as GameObject;
enemyObject.name = "Enemy";
enemyObject.tag = "Enemy";
enemyObject.transform.parent = transform;
}
这是预制脚本的代码
void Start()
{
rigidbody2DComponent = GetComponent<Rigidbody2D>();
initialYPosition = transform.position.y;
Move();
}
void Move()
{
speed = Random.Range(-10, -20);
rigidbody2DComponent.velocity = new Vector2(0, speed);
//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);
}
transform.position = new Vector3(newXPosition, initialYPosition, transform.position.z);
}
public void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player" || other.tag == "resetWall")
{
//Lets let our master script take care of spawning
Enemy.SpawnEnemy.Spawn();
//Let's let our master script handle this
//Without changing the name, the original name will get a bunch of
//(clone) added to it as it respawns
//newEnemyObject.name = "newEnemy";
//newEnemyObject.tag = "Enemy";
//Destroy the old enemy
Destroy(gameObject);
}
}