从下一个实例化更改实例化游戏对象的移动

我正在使用 Unity 和 C#。


在我的游戏中,敌人被反复实例化并以线性路径向玩家移动。我想让敌人在经过特定时间后以正弦波移动,以使游戏更加艰难。


运动会立即发生变化,但我希望这种变化从新(下一个)敌人的实例化而不是当前存在的敌人的实例化生效。


我能做什么?


EnemySpawner 脚本 -


private void Start()

{

    timeelapsed = 0f;

    StartCoroutine(SpawnEnemies(delay));

}

private void Update()

{

    timeelapsed += Time.deltaTime;

}

private IEnumerator SpawnEnemies(float delay)

{

    while (true)

    {

        SpawnNewEnemy();

        yield return new WaitForSeconds(delay);

    }

}

private void SpawnNewEnemy()

{

    if (!enemyclone)

    {

        enemyclone = Instantiate(enemy, enemySpawner.transform.position + offset, Quaternion.identity);

    }

}

Enemy Movement 脚本:


private void Update()

{

    t = Time.time;

    if (EnemySpawner.timeelapsed > 0f && EnemySpawner.timeelapsed <= 20f)

    {

        enemy.transform.Translate(Vector3.forward * speed * Time.deltaTime);

    }

    if (EnemySpawner.timeelapsed > 20f && EnemySpawner.timeelapsed <= 40f)

    {

        enemy.GetComponent<Rigidbody>().velocity = Vector3.forward * 5 + Vector3.left * 3 * Mathf.Sin(10f * t);

    }

}


qq_遁去的一_1
浏览 156回答 2
2回答

富国沪深

有上千种方法可以实现这一点,但我认为可能易于实现和理解的一种方法是设置 Action,当您从 spawner 实例化一个新敌人时会触发它。我很快就为您的 EnemySpawner 和 EnemyMovement 脚本编写了一些我认为可能对您有所帮助的更改。首先,EnemySpawner:public static event Action EnemySpawning;void SpawnNewEnemy ( ){&nbsp; &nbsp; if ( !enemyclone )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; EnemySpawning?.Invoke ( );&nbsp; &nbsp; &nbsp; &nbsp; enemyclone = Instantiate ( enemy, enemySpawner.transform.position + offset, Quaternion.identity );&nbsp; &nbsp; }}现在,敌人运动:private bool travelInLinearPath = true;private void OnEnable ( ){&nbsp; &nbsp; EnemySpawner.EnemySpawning -= OnEnemySpawning;&nbsp; &nbsp; EnemySpawner.EnemySpawning += OnEnemySpawning;&nbsp; &nbsp; travelInLinearPath = true;}public void OnEnemySpawning( ){&nbsp; &nbsp; EnemySpawner.EnemySpawning-= OnEnemySpawning;&nbsp; &nbsp; travelInLinearPath = false;}private void Update ( ){&nbsp; &nbsp; if ( travelInLinearPath )&nbsp; &nbsp; &nbsp; &nbsp; enemy.transform.Translate ( Vector3.forward * speed * Time.deltaTime );&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; enemy.GetComponent<Rigidbody> ( ).velocity = Vector3.forward * 5 + Vector3.left * 3 * Mathf.Sin ( 10f * Time.time );}每次启用敌人时,您都要求在下一个敌人即将产生时收到通知。发生这种情况时,您告诉这个敌人从线性运动变为正弦运动 ( travelInLinearPath)。您还要求在产生新敌人时不再通知该敌人(除非您重新启用该敌人)。我也没有讨论你是否应该从敌人对象池中抓取一个新的敌人。但希望你这样做是我在OnEnable(). 您可能希望/需要将该代码放入其中,Start()这取决于您在游戏后期的敌人实例化和创建。

慕村9548890

现在知道请求是针对敌人的“波浪”,我们需要向每个敌人添加波浪定义项目。因此,首先修改EnemyMovement脚本:public class EnemyMovement : MonoBehaviour{&nbsp; &nbsp; public enum MovementType&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; None = 0,&nbsp; &nbsp; &nbsp; &nbsp; Linear = 1,&nbsp; &nbsp; &nbsp; &nbsp; Sinusoidal = 2&nbsp; &nbsp; &nbsp; &nbsp; // etc, etcc&nbsp; &nbsp; }&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// Movement affects the movement type of the enemy.&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; public MovementType Movement { get; set; }&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// This affects the Speed of the Enemy. Used in conunction with Movement to&nbsp; &nbsp; /// produce the enenmy's wave movement type.&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; public float Speed { get; set; }&nbsp; &nbsp; private Rigidbody rigidBody;&nbsp; &nbsp; private void Awake ( )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; rigidBody = GetComponent<Rigidbody> ( );&nbsp; &nbsp; }&nbsp; &nbsp; private void Update ( )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; switch ( Movement)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case MovementType.Linear:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transform.Translate ( Vector3.forward * Speed * Time.deltaTime );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case MovementType.Sinusoidal:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // You probably want the Speed property to affect this as well...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rigidBody.velocity = Vector3.forward * 5 + Vector3.left * 3 * Mathf.Sin ( 10f * Time.time );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Any extra movement types you want here...&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}然后是EnemySpawner驱动 Enemy 实例化的脚本:public class EnemySpawner : MonoBehaviour{&nbsp; &nbsp; public float Delay;&nbsp; &nbsp; public float StartSpeed;&nbsp; &nbsp; public GameObject enemy;&nbsp; &nbsp; public GameObject enemySpawner;&nbsp; &nbsp; public Vector3 offset;&nbsp; &nbsp; private float timeElapsed;&nbsp; &nbsp; private float currentSpeed;&nbsp; &nbsp; private EnemyMovement.MovementType currentMovement;&nbsp; &nbsp; void Start ( )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; timeElapsed = 0f;&nbsp; &nbsp; &nbsp; &nbsp; currentSpeed = StartSpeed;&nbsp; &nbsp; &nbsp; &nbsp; StartCoroutine ( SpawnEnemies ( Delay ) );&nbsp; &nbsp; &nbsp; &nbsp; currentMovement = EnemyMovement.MovementType.Linear;&nbsp; &nbsp; }&nbsp; &nbsp; void Update ( )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; timeElapsed += Time.deltaTime;&nbsp; &nbsp; &nbsp; &nbsp; // We can determine at what time the Wave parameters change here.&nbsp; &nbsp; &nbsp; &nbsp; if ( timeElapsed >= 40.0f )&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentSpeed += 10.0f; // Add speed, for example.&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if ( timeElapsed >= 20.0f )&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentMovement = EnemyMovement.MovementType.Sinusoidal;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; IEnumerator SpawnEnemies ( float delay )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; while ( true )&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var enemyClone = Instantiate ( enemy, enemySpawner.transform.position + offset, Quaternion.identity );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var movement = enemyClone.GetComponent<EnemyMovement> ( );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // We now set what the enemy uses as the Wave values.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; movement.Speed = currentSpeed;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; movement.Movement = currentMovement;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return new WaitForSeconds ( delay );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP