【发布时间】:2018-11-06 04:45:51
【问题描述】:
我正在尝试创建一个可以将玩家从一端带到另一端的门户。因此,如果玩家从一端进入传送门的 50%,他的另外 50% 将从另一端伸出。
我正在使用具有完全相同组件的完全相同精灵的两个预制角色。门户将仅使用这两个预制角色,而不是证实新的克隆。
澄清一下,我正在使用 OnCollisionEnter2D 检测接触点,以确定玩家从门户的哪一侧进入。如上图右侧所示,“进来的人”应该可以随时转身不被撤走,而从另一边离开时仍然可以被撤走。
但是,我的代码面临的问题是 OnCollisionEnter2D 函数仅在 IsTrigger 为 false 时才有效。但是当 IsTrigger 为 false 时,我的角色无法通过传送门并被推入地面并从地图上掉下来。
我尝试使用 Physics2D.IgnoreCollision,但似乎没有效果。
第二个被移动到出口传送门的角色被推出而不是平滑过渡。
public Transform PortalExit;
public CharacterController2D Player;
private GameObject Player1;
private CharacterController2D Player1Controller;
private Vector2 Player1Position;
private GameObject Player2;
private CharacterController2D Player2Controller;
private Vector2 Player2Position;
// Use this for initialization
void Start () {
Player1 = GameObject.FindWithTag("Player1");
Player1Controller = GameObject.FindWithTag("Player1").GetComponent<CharacterController2D>();
Player1Position = Player1.transform.position;
Player2 = GameObject.FindWithTag("Player2");
Player2Controller = GameObject.FindWithTag("Player2").GetComponent<CharacterController2D>();
Player2Position = Player2.transform.position;
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D (Collision2D collision) {
Collider2D collider = collision.collider;
if (collider.gameObject.tag == "Player1") {
Vector3 contactPoint = collision.contacts[0].point;
Vector3 center = collider.bounds.center;
bool right = contactPoint.x > center.x;
bool top = contactPoint.y > center.y;
if (right) {
// Physics2D.IgnoreCollision(GameObject.FindWithTag("Player1").GetComponent<Collider2D>(), GetComponent<Collider2D>());
// change Player2's position into the other portal
print("Hello!");
Player2Position.x = 1.6f;
Player2Position.y = PortalExit.position.y;
Player2.transform.position = Player2Position;
}
if (!right) {
print("Goodbye!");
// puts Player1 into the detention center
Player1Position.x = -5.55f;
Player1Position.y = -6f;
Player1.transform.position = Player1Position;
}
}
if (collider.gameObject.tag == "Player2") {
Vector3 contactPoint = collision.contacts[0].point;
Vector3 center = collider.bounds.center;
bool right = contactPoint.x > center.x;
bool top = contactPoint.y > center.y;
if (!right) {
// change Player2's position into the other portal
Player1Position.x = -3.6f;
Player1Position.y = PortalExit.position.y;
Player1.transform.position = Player1Position;
}
if (right) {
// puts Player1 into the detention center
Player2Position.x = -5.55f;
Player2Position.y = -6f;
Player2.transform.position = Player2Position;
}
}
}
【问题讨论】:
-
我最近在我的赛车游戏的检查站和部分终点试验了用于高速计时器的 2D 边缘对撞机。它们非常准确。比我预期的要多得多。我认为他们会遇到隧道问题和缺少高速交互。他们没有。它们可以完全像无限厚的 2D 形状一样使用。这意味着您可以在角色的最边缘使用它们来确定他相对于门户的位置,并在他完全通过时发出信号,在任一方向。垂直使用它们,连接到角色的边缘。有意义吗?
-
@Confusde 对不起,我不明白你的建议......
-
我需要画一些照片...你能等一下吗?我将在接下来的几天内为我自己的项目做一些事情,这样我就可以画出非常具体的东西了。
-
我的是隐形传送,有些效果需要同时出现在两个地方。尽管有传送,粒子、轨迹效果、声音和其他东西仍需要继续,因为它是一圈末端传送,它应该是无缝的并且对玩家来说是不明显的。所以我们有相似的需求,原因却大相径庭。
-
好的!我很高兴看到你的照片。