Unity 5 碰撞仍在发生
Unity 5 collision still occuring
我正在开发一款 2D 平面躲避游戏,但我在 Unity 5 中遇到问题,我试图阻止两架进入的飞机之间的碰撞,我尝试了很多不同的解决方案,但那些都没有工作。我不能将它们放在不同的层中,因为它们仍然需要与玩家发生碰撞。这是我的第一架飞机的代码。 Enemy 和 Bullets 都是标签。谢谢
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);
}
}
}
您仍然可以设置图层,而您的玩家仍然可以与之互动。
假设您有 3 个不同的对象
- 平面A
- B机
- 玩家
并且您希望 Plane A 不与 Plane B 相撞,但仍与 Player 相撞。
你可以设置碰撞矩阵
编辑 -> 项目设置 -> Physics2D
如何设置?
- 将平面A设为图层'CollideOnlyWithPlayer'
- 将播放器设置到层 'Player'
- 然后在碰撞矩阵中,你可以取消选中所有层CollideOnlyWithPlayer
- 但是检查 CollideWithPlayer to Player 层
碰撞矩阵定义对象如何与图层中的其他对象相互作用。
它由两个索引定义,垂直索引和水平索引。
例如,当您检查时,垂直 'Player' 到水平 'Player'
这意味着,您的玩家可以与玩家互动
例如,当您检查时,垂直 'Player' 到水平 'CollideWithPlayer'
这意味着您的玩家物理可以与分层到 CollideWithPlayer 的对象交互。
请在此处查看完整文档
https://docs.unity3d.com/Manual/LayerBasedCollision.html
我正在开发一款 2D 平面躲避游戏,但我在 Unity 5 中遇到问题,我试图阻止两架进入的飞机之间的碰撞,我尝试了很多不同的解决方案,但那些都没有工作。我不能将它们放在不同的层中,因为它们仍然需要与玩家发生碰撞。这是我的第一架飞机的代码。 Enemy 和 Bullets 都是标签。谢谢
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);
}
}
}
您仍然可以设置图层,而您的玩家仍然可以与之互动。
假设您有 3 个不同的对象
- 平面A
- B机
- 玩家
并且您希望 Plane A 不与 Plane B 相撞,但仍与 Player 相撞。
你可以设置碰撞矩阵
编辑 -> 项目设置 -> Physics2D
如何设置?
- 将平面A设为图层'CollideOnlyWithPlayer'
- 将播放器设置到层 'Player'
- 然后在碰撞矩阵中,你可以取消选中所有层CollideOnlyWithPlayer
- 但是检查 CollideWithPlayer to Player 层
碰撞矩阵定义对象如何与图层中的其他对象相互作用。 它由两个索引定义,垂直索引和水平索引。 例如,当您检查时,垂直 'Player' 到水平 'Player' 这意味着,您的玩家可以与玩家互动
例如,当您检查时,垂直 'Player' 到水平 'CollideWithPlayer' 这意味着您的玩家物理可以与分层到 CollideWithPlayer 的对象交互。
请在此处查看完整文档 https://docs.unity3d.com/Manual/LayerBasedCollision.html