Quantcast
Channel: Questions in topic: "physics 2d"
Viewing all articles
Browse latest Browse all 132

OnCollsionExit2D gets invoked wrongly even when the character is colliding with the object

$
0
0
Hi All, This is my first post. I am trying to use Physics2D.IgnoreLayerCollision to ignore collision between the player and the platform when the player jumps from under it. It works fine without any issues. But when the platform is moving the code that I added for making the player to stick on the platform does not work. Below is the Player update method: void Update(){ if (transform.position.y <= fallBoundary) { damagePlayer (99999); } //_vy is the velocity of the player in the y direction _vy = r2d.velocity.y; //ignore collision when jumping from below the platform Physics2D.IgnoreLayerCollision(_playerLayer, _platformLayer, (_vy > 0.0f)); } I add the speed of the moving platform to the player when he is standing on it by using OnCollisionStay2D method and when the player leaves the platform I remove the speed by using OnCollsionExit2D method. The problem is that OnCollisionExit2D gets called even when the player is standing on the platform(**Note**:This does not happen when I dont use Physics2D.ignoreLayerCollsion). Below are the OnCollsionStay2D and OnCollisionExit2D methods: void OnCollisionStay2D(Collision2D colliderInfo){ //Debug.Log ("moving"); if (colliderInfo == null || colliderInfo.gameObject == null) { Debug.Log ("return"); return; } if (colliderInfo.gameObject.tag.Equals ("Player")) { PlatformerCharacter2D d = player.GetComponent (); if (rightToleft) { //Debug.Log ("rightToleft"); d.platformSpeed = -xvel; } else { //Debug.Log ("righttoleftfalse"); d.platformSpeed = xvel; } } } void OnCollisionExit2D(Collision2D colliderInfo){ PlatformerCharacter2D d = player.gameObject.GetComponent (); d.platformSpeed=0f; } I am just curious as to why this happens. Thanks in advance for the help.

Viewing all articles
Browse latest Browse all 132

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>