简体   繁体   English

Unity 3D如何使具有不同对象的触发器/碰撞发生不同的事情? C#

[英]Unity 3D how to make different things happen for triggers/collisions with different objects? C#

I have code for what happens when the enemy collides with the player using a rigid body on the player and clicking the is trigger on the collider in the inspector. 我有代码说明当敌人使用播放器上的刚体与播放器碰撞并单击检查器中对撞机上的is触发器时会发生什么。 When the player hits the enemy, the number of lives go down. 当玩家击中敌人时,生命减少。 I now have an empty game object (track) which has a rigid body and ticked the is trigger on the box collider, that one enemy moves toward. 现在,我有一个空的游戏对象(轨迹),它具有刚硬的身体,并在盒子对撞机上勾选了is触发器,一个敌人向该物体移动。 Once the enemy gets to the trigger, it is supposed to move back towards another empty game object (backTrack) to make it look like it is walking back again. 一旦敌人到达了扳机,它就应该移向另一个空的游戏对象(backTrack),使其看起来像是再次向后走。 At any point, if the player hits the enemy, the life count goes down by 1 and the enemy respawns at the start position. 在任何时候,如果玩家击中敌人,生命值都会减少1,并且敌人会在起始位置重生。 My problem is that I can't get it to distinguish between colliding with the player and track because at the minute, when the enemy hits track, it does what should be done if the player had hit the enemy and the enemy respawns at start position. 我的问题是我无法区分是与玩家碰撞还是与轨道碰撞,因为在敌人击中轨道的那一刻,如果玩家击中了敌人并且敌人在起始位置重生,那该怎么办。 Here is my code: 这是我的代码:

 private bool triggered = false;
 public GameObject track1;
 void moveBackTrack(){
     float move1 = moveSpeed * Time.deltaTime;
     transform.position = Vector3.MoveTowards (transform.position, backTrack.position, move1);
 }
 void OnTriggerEnter(Collider col)
 {
     if (!triggered)
     {
         triggered = true;
         if (col.gameObject = track1) {
             moveBackTrack ();
         } else {
             Destroy (gameObject);
             print ("Collision Enter: " + col.name);
             GameManager.Instance.lives -= 1;
             GameManager.Instance.setLivesText ();
             GameManager.Instance.SpawnTrackEnemy ();
         }
     }
 }

Set a tag ID for the player and enemy objects, then subscribe to the OnCollisionenter event of your track. 设置玩家和敌人对象的标签ID,然后订阅轨迹的OnCollisionenter事件。 This will fire when any collision occurs between another object and the collider of your track object. 当另一个对象与跟踪对象的碰撞器之间发生任何碰撞时,将触发此事件。

void OnCollisionEnter(Collision col){
    GameObject collidedWith = col.gameObject;
    if(collidedWith.tag == "PlayerTagName"){
        //do player collided logic here
    }
    else if(collidedWith.tag == "EnemyTagName"){
        //do enemy collided logic here
    }
}

By handling each object by its Tag name individually, you can better maintain your code, making it easier to code to each separate condition. 通过按其标记名分别处理每个对象,可以更好地维护代码,从而更轻松地对每个单独的条件进行编码。 I recommend setting up a similar OnCollissionEnter handler for your player and enemies as well, depending on how many conditions might occur in the logic of your game. 我建议也为您的玩家和敌人设置一个类似的OnCollissionEnter处理程序,具体取决于游戏逻辑中可能发生的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM