我正在尝试创建一个门户,可以将玩家从一端带到另一端。因此,如果玩家处于门户的50%位置,其另外50%将从另一端突出。
我使用了两个完全相同的预制角色和完全相同的组件。门户将仅使用这两个预制字符,而不会实例化新的克隆体。
下图描述了我正尝试在Collider盒中使用的内容以移动它们:
只是为了澄清,我正在使用OnCollisionEnter2D检测接触点,以确定玩家从哪一侧进入门户。如上图右侧所示,“进入”的那个人应该能够随时回头并且不被删除,同时在通过另一侧离开时仍然能够被删除。
但是,我在我的代码中遇到的问题是当IsTrigger为false时,OnCollisionEnter2D函数仅适用于工作。但是当IsTrigger为false时,我的角色无法穿过门户,并被推入地面并掉出地图。
我曾尝试使用Physics2D.IgnoreCollision,但似乎没有效果。
第二个被移动到退出门户的角色被弹出,而不是具有平滑的过渡。
我使用了两个完全相同的预制角色和完全相同的组件。门户将仅使用这两个预制字符,而不会实例化新的克隆体。
下图描述了我正尝试在Collider盒中使用的内容以移动它们:
![Explanation](https://istack.dev59.com/bkpUf.webp)
但是,我在我的代码中遇到的问题是当IsTrigger为false时,OnCollisionEnter2D函数仅适用于工作。但是当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;
}
}
}