Unity 5碰撞仍然存在

时间:2017-11-22 22:50:17

标签: unity3d c#-4.0 unity5 collision-detection

我正在制作2D平面躲避游戏,而我在Unity 5中遇到问题,我试图阻止两架飞机之间的碰撞,我尝试了很多不同的解决方案,但那些不起作用。我无法将它们放在不同的层中,因为它们仍然需要与播放器发生冲突。这是我对第一架飞机的代码。敌人和子弹都是标签。感谢

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletPlane : MonoBehaviour {
    public float speed = 6f;


    public float reloadTime = 1f;
    public GameObject bulletPrefab;
    public GameObject explosion;
    public GameObject smoke;
    public GameObject warning;
    public GameObject self;

    public float reloadSecond = .10f;


    private float elapsedTime = 0;
    public float timeElapsed = 0;

    private Rigidbody2D rigidBody;

    void Start()
    {
        rigidBody = GetComponent<Rigidbody2D> ();
        rigidBody.velocity = new Vector2 (0, speed);
        Vector3 warningPos = transform.position;
        warningPos += new Vector3 (0, -10.5f, 0);
        Instantiate (warning, warningPos, Quaternion.identity);
    }
    void Update() {
        elapsedTime++;
            Vector3 spawnPos = transform.position;
            Vector3 secondPos = transform.position;
            elapsedTime = 0f;
            spawnPos += new Vector3 (0, -1.2f, 0);
            secondPos += new Vector3 (-1, -0.9f, 0);
            Instantiate (bulletPrefab, spawnPos, Quaternion.identity);
            Instantiate (smoke, secondPos, Quaternion.identity);
            if (elapsedTime > reloadSecond) {
                spawnPos += new Vector3 (0, -1.2f, 0);
                secondPos += new Vector3 (-1, -0.9f, 0);
                Instantiate (bulletPrefab, spawnPos, Quaternion.identity);
                Instantiate (smoke, secondPos, Quaternion.identity);

                elapsedTime = 0f;
                timeElapsed++;
        }
    }
    void OnTriggerEnter2D(Collider2D other) {
        if (other.gameObject.CompareTag ("Player")) {
            Destroy (other.gameObject);
        } else if (other.gameObject.CompareTag ("Enemy")) {
            Physics2D.IgnoreCollision (other.GetComponent<Collider2D> (), gameObject.GetComponent<Collider2D> (), true);
        } else if (other.gameObject.CompareTag ("Bullets")) {
            Physics2D.IgnoreCollision (other.GetComponent<Collider2D> (), gameObject.GetComponent<Collider2D> (), true);
        }
    }
}

这是另一架敌机的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyPlane : MonoBehaviour {
    public float speed = -6f;


    public float reloadTime = 1f;
    public GameObject bulletPrefab;
    public GameObject explosion;


    private float elapsedTime = 0;

    private Rigidbody2D rigidBody;

    void Start()
    {
        rigidBody = GetComponent<Rigidbody2D> ();
        rigidBody.velocity = new Vector2 (0, speed);
    }
    void OnTriggerEnter2D(Collider2D other) {
        if (other.gameObject.CompareTag ("Ally")) {

        } else if (other.gameObject.CompareTag ("Enemy")) {
            Physics2D.IgnoreCollision (other.GetComponent<Collider2D> (), gameObject.GetComponent<Collider2D> (), true);
        } else if (other.gameObject.CompareTag ("BulletPlane")) {
            Physics2D.IgnoreCollision (other.GetComponent<Collider2D> (), gameObject.GetComponent<Collider2D> (), true);
}
}
}

1 个答案:

答案 0 :(得分:0)

您仍然可以设置图层,而您的播放器仍然可以与之互动。

假设您有3个不同的对象

  1. 飞机A
  2. 飞机B
  3. 播放器
  4. 并且您希望平面A不与平面B发生碰撞,但仍然与玩家碰撞。

    您可以在
    上设置碰撞矩阵 编辑 - &gt;项目设置 - &gt; Physics2D

    如何设置?

    1. 将平面A设置为图层'CollideOnlyWithPlayer'
    2. 将播放器设置为图层'播放器'
    3. 然后在碰撞矩阵中,您可以取消所有图层CollideOnlyWithPlayer
    4. 但请检查CollideWithPlayer到播放器图层
    5. 碰撞矩阵定义对象如何在图层中的另一个对象内进行交互。 它由两个索引定义,垂直索引和水平索引。 当您检查例如,垂直'播放器'到水平'播放器' 这意味着,您的玩家可以与玩家进行互动

      当您检查例如,垂直'播放器'到水平'CollideWithPlayer' 这意味着您的播放器物理可以​​与分层到CollideWithPlayer的对象进行交互。

      请在此处查看完整的文档 https://docs.unity3d.com/Manual/LayerBasedCollision.html