private void LateUpdate() { mLastFramePosition = this.transform.position; if (mHitLeft && VelX < 0) { VelX = 0; } else if (mHitRight && VelX > 0) { VelX = 0; } #if (UNITY_EDITOR) // draw the collider each frame. JCS_Debug.DrawCollider(mBoxCollider2d, Color.cyan); JCS_Debug.DrawCollider(mBoxCollider2d, Color.blue, mLastFrameColliderPosition); #endif // record down the last frame's collider's position mLastFrameColliderPosition = JCS_Physics.GetColliderPosition(mBoxCollider2d); }
/// <summary> /// Draw the collider /// </summary> /// <param name="collider"> Collider u want to draw. </param> /// <param name="col"> Color type. </param> public static void DrawCollider(BoxCollider2D collider, Color col) { #if (UNITY_EDITOR) // get width and height information. Vector2 boxInfo = JCS_Physics.GetColliderInfo(collider); Vector3 pos = JCS_Physics.GetColliderPosition(collider); Vector3 topLeft = new Vector3( pos.x - boxInfo.x / 2, pos.y + boxInfo.y / 2); Vector3 topRight = new Vector3( pos.x + boxInfo.x / 2, pos.y + boxInfo.y / 2); Vector3 botRight = new Vector3( pos.x + boxInfo.x / 2, pos.y - boxInfo.y / 2); Vector3 botLeft = new Vector3( pos.x - boxInfo.x / 2, pos.y - boxInfo.y / 2); DrawRect(topLeft, topRight, botRight, botLeft, col); #endif }
// OPTIMIZE(jenchieh): no idea why on trigger enter // wont set on top of the collider. private void OnTriggerEnter2D(Collider2D other) { // check ray ignore. if (other.GetComponent <JCS_RayIgnore>() != null) { return; } if (mZeroRotationWhenIsTrigger) { this.transform.eulerAngles = Vector3.zero; } // Detect Right { Vector3 right = transform.TransformDirection(Vector3.right); RaycastHit2D[] hits = Physics2D.RaycastAll( JCS_Physics.GetColliderPosition(mBoxCollider2d), right, mBoxInfo.x / 2 + mDetectDistance); #if (UNITY_EDITOR) //Debug.DrawRay(transform.position, right, Color.green); #endif foreach (RaycastHit2D hit in hits) { BoxCollider2D bc2d = hit.transform.GetComponent <BoxCollider2D>(); // ignore the tag. if (hit.transform.GetComponent <JCS_RayIgnore>() != null || bc2d == null || hit.transform == this.transform || hit.transform != other.transform) { continue; } mHitRight = true; mVelocity.x = 0; Vector3 newPos = this.transform.position; newPos.x = mCurrentFrame.x; this.transform.position = newPos; // fix collision. JCS_Physics.SetOnLeftOfBox(mBoxCollider2d, bc2d); // check if colllider in array already bool found = false; foreach (Collider2D temp in mRightColliders) { if (temp.transform == other.transform) { found = true; } } // if not found. if (!found) { mRightColliders.Add(other); } return; } } // Detect Left { Vector3 left = transform.TransformDirection(Vector3.left); RaycastHit2D[] hits = Physics2D.RaycastAll( JCS_Physics.GetColliderPosition(mBoxCollider2d), left, mBoxInfo.x / 2 + mDetectDistance); #if (UNITY_EDITOR) //Debug.DrawRay(transform.position, left, Color.green); #endif foreach (RaycastHit2D hit in hits) { BoxCollider2D bc2d = hit.transform.GetComponent <BoxCollider2D>(); // ignore the tag. if (hit.transform.GetComponent <JCS_RayIgnore>() != null || bc2d == null || hit.transform == this.transform || hit.transform != other.transform) { continue; } mHitLeft = true; mVelocity.x = 0; Vector3 newPos = this.transform.position; newPos.x = mCurrentFrame.x; this.transform.position = newPos; // fix collision. JCS_Physics.SetOnRightOfBox(mBoxCollider2d, bc2d); // check if colllider in array already bool found = false; foreach (Collider2D temp in mLeftColliders) { if (temp.transform == other.transform) { found = true; } } // if not found. if (!found) { mLeftColliders.Add(other); } return; } } // Detect bottom { Vector3 down = transform.TransformDirection(Vector3.down); // NOTE(jenchieh): box info .x are the same as saying the width. //RaycastHit2D[] hits = Physics2D.RaycastAll(transform.position, down, mBoxInfo.y / 2 + mDetectDistance); RaycastHit2D[] hits = Physics2D.CircleCastAll( JCS_Physics.GetColliderPosition(mBoxCollider2d), mBoxInfo.x, down, mBoxInfo.y / 2 + mDetectDistance); #if (UNITY_EDITOR) //Debug.DrawRay(transform.position, down, Color.green); #endif foreach (RaycastHit2D hit in hits) { BoxCollider2D bc2d = hit.transform.GetComponent <BoxCollider2D>(); // ignore the tag. if (hit.transform.GetComponent <JCS_RayIgnore>() != null || bc2d == null || hit.transform == this.transform || hit.transform != other.transform) { continue; } mHitBottom = true; Vector3 newPos = this.transform.position; newPos.y = mCurrentFrame.y; this.transform.position = newPos; // fixed collision JCS_Physics.SetOnTopOfBox(mBoxCollider2d, bc2d); // check if colllider in array already bool found = false; foreach (Collider2D temp in mBottomColliders) { if (temp.transform == other.transform) { found = true; } } // if not found. if (!found) { mBottomColliders.Add(other); } break; } } // Detect top { Vector3 top = transform.TransformDirection(Vector3.up); // NOTE(jenchieh): box info .x are the same as saying the width. RaycastHit2D[] hits = Physics2D.RaycastAll( JCS_Physics.GetColliderPosition(mBoxCollider2d), top, mBoxInfo.y / 2 + mDetectDistance); #if (UNITY_EDITOR) //Debug.DrawRay(transform.position, top, Color.green); #endif foreach (RaycastHit2D hit in hits) { BoxCollider2D bc2d = hit.transform.GetComponent <BoxCollider2D>(); // ignore the tag. if (hit.transform.GetComponent <JCS_RayIgnore>() != null || bc2d == null || hit.transform == this.transform) { continue; } mHitTop = true; Vector3 newPos = this.transform.position; newPos.y = mCurrentFrame.y; this.transform.position = newPos; // fixed collision JCS_Physics.SetOnBottomOfBox(mBoxCollider2d, bc2d); // check if colllider in array already bool found = false; foreach (Collider2D temp in mTopColliders) { if (temp.transform == other.transform) { found = true; } } // if not found. if (!found) { mTopColliders.Add(other); } break; } } if (!mHitLeft && !mHitRight) { // record down the frame. mCurrentFrame = this.transform.position; } }