【问题标题】:Unity 5 collision still occuringUnity 5 碰撞仍在发生
【发布时间】:2017-11-22 22:50:17
【问题描述】:

我正在开发一款 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);
}
}
}

【问题讨论】:

  • 编辑 -> 项目设置 -> Physics2D。在此设置面板(底部)中,您可以将图层默认设置为非碰撞。
  • @Draco18s 这不起作用,因为我仍然需要它们与播放器发生碰撞
  • ...这就是碰撞网格的用途...您可以将平面图层设置为不与自身或您想要的任何其他图层碰撞,但仍与播放器层。

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


【解决方案1】:

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

假设你有 3 个不同的对象

  1. A 面
  2. 平面 B
  3. 播放器

并且您希望平面 A 不与平面 B 相撞,但仍与玩家相撞。

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

如何设置?

  1. 将平面 A 设置为“CollideOnlyWithPlayer”层
  2. 将播放器设置为“播放器”层
  3. 然后在碰撞矩阵中,你可以取消所有层 CollideOnlyWithPlayer
  4. 但是检查 CollideWithPlayer 到 Player 层

碰撞矩阵定义对象如何在层中的其他对象中交互。 它由两个索引定义,垂直索引和水平索引。 例如,当您检查垂直“播放器”到水平“播放器”时 这意味着,您的播放器可以与播放器互动

例如,当您检查垂直“播放器”到水平“碰撞与播放器”时 这意味着您的玩家物理可以与 CollideWithPlayer 分层的对象交互。

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

【讨论】:

  • 最后一个问题,我已经制作了适合每个玩家、敌人和敌人子弹的额外层。我已经用正在生成的敌人适当地标记了每个(将图层添加到正在生成的对象中)并检查了诸如敌人/敌人之类的东西,但它们仍在碰撞?
  • 如果你想让敌人不与敌人发生碰撞,那么你必须在碰撞矩阵中取消选中敌人。
  • 我做到了,我什至尝试为不同类型的敌人制作一个单独的,并取消它们彼此的检查,他们仍然在碰撞
  • 确保你设置了正确的碰撞矩阵,有 2 种碰撞类型,2D 和 3D 设置 2D 碰撞矩阵去编辑 -> 项目设置 -> Physics2D 并确保你的对象使用正确的层。如果,你确信一切都很好,但仍然有问题,你可以给我更详细的游戏结构,我很乐意提供帮助
  • 还要确保将图层应用于其中包含碰撞器组件的对象
猜你喜欢
  • 2018-08-25
  • 2023-02-23
  • 1970-01-01
  • 2022-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多