protected virtual void OnTriggerEnter(Collider other) { JCS_2DSideScrollerPlayer player = other.GetComponent <JCS_2DSideScrollerPlayer>(); if (player == null) { return; } AddSafe(player); bool isTopOfBox = JCS_Physics.TopOfBox( player.GetCharacterController(), mPositionPlatform.GetPlatformCollider()); if (isTopOfBox) { // show character behind the ladder int backOrderLayer = OrderLayerObject.sortingOrder - ClimbableManager.SORTING_ORDER_BEHIND_OFFSET; SetPlayerSortingOrder(player, backOrderLayer); } else { // show character infront int frontOrderLayer = OrderLayerObject.sortingOrder + ClimbableManager.SORTING_ORDER_INFRONT_OFFSET; SetPlayerSortingOrder(player, frontOrderLayer); } player.CanLadder = true; player.CanRope = false; player.ClimbableObject = this; }
//---------------------- // Protected Functions //---------------------- // Private Functions /// <summary> /// Only check when the item start dropping. /// </summary> /// <param name="other"> collider detected. </param> private void TriggerDropping(Collider other) { if (mVelocity.y > 0) { return; } // meet ignore object JCS_ItemIgnore jcsii = other.GetComponent <JCS_ItemIgnore>(); if (jcsii != null) { return; } JCS_Item otherItem = this.GetComponent <JCS_Item>(); // if itself it a item, we check other is a item or not. if (otherItem != null) { otherItem = other.GetComponent <JCS_Item>(); // if both are item then we dont bother // each other action. if (otherItem != null) { return; } } #if (UNITY_EDITOR) // if is debug mode print this out. // in order to know what does item touched and // stop this movement. if (JCS_GameSettings.instance.DEBUG_MODE) { JCS_Debug.PrintName(other.transform); } #endif mVelocity.y = 0; mEffect = false; mFixCollider = other; // TODO(JenChieh): not all the object we get set are // box collider only. BoxCollider beSetBox = other.GetComponent <BoxCollider>(); // set this ontop of the other box(ground) if (beSetBox != null) { JCS_Physics.SetOnTopOfBoxWithSlope(mBoxCollider, beSetBox); } // enable the physic once on the ground JCS_PlayerManager.instance.IgnorePhysicsToAllPlayer(this.mBoxCollider, false); }
/* Functions */ private void Awake() { mCharacterController = this.GetComponent <CharacterController>(); Vector2 widthHeight = JCS_Physics.GetColliderWidthHeight(mCharacterController); mWidth = widthHeight.x; mHeight = widthHeight.y; }
/// <summary> /// Check if is on top of the lean platform. /// </summary> /// <param name="player"></param> /// <returns> /// true: is on top of the box. /// false: vice versa. /// </returns> public virtual bool IsOpTopOfLeanPlatform(JCS_Player player) { if (mPositionPlatform == null || player == null) { return(false); } bool isTopOfBox = JCS_Physics.TopOfBox( player.GetCharacterController(), mPositionPlatform.GetPlatformCollider()); return(isTopOfBox); }
/* Functions */ private void Awake() { mBoxCollider2d = this.GetComponent <BoxCollider2D>(); mRigidbody2d = this.GetComponent <Rigidbody2D>(); mSpriteRenderer = this.GetComponent <SpriteRenderer>(); // get the box info. mBoxInfo = JCS_Physics.GetColliderInfo(mBoxCollider2d); // set to never sleep mRigidbody2d.sleepMode = RigidbodySleepMode2D.NeverSleep; mBottomColliders = new List <Collider2D>(); mTopColliders = new List <Collider2D>(); mRightColliders = new List <Collider2D>(); mLeftColliders = new List <Collider2D>(); }
/// <summary> /// Something that needed to check in update can be design here. /// This function should get call by when the player is done climbing. /// </summary> public override void ClimbableUpdate() { JCS_ClimbableManager cm = JCS_ClimbableManager.instance; foreach (JCS_2DSideScrollerPlayer player in mSSPlayers) { if (player.isGrounded()) { player.CanRope = true; player.CanLadder = false; player.ClimbableObject = this; continue; } if (player.CharacterState == JCS_2DCharacterState.CLIMBING && player.ClimbMoveType == JCS_ClimbMoveType.MOVE_UP) { // show character infront int frontOrderLayer = OrderLayerObject.sortingOrder + cm.SORTING_ORDER_INFRONT_OFFSET; SetPlayerSortingOrder(player, frontOrderLayer); bool isTopOfBox = JCS_Physics.TopOfBox( player.GetCharacterController(), mPositionPlatform.GetPlatformCollider()); /* Check top of the platform */ if (isTopOfBox) { player.ClimbableObject = null; player.GetCharacterAnimator().PlayAnimationInFrame(); player.CanRope = false; player.VelY = 0; player.JustClimbOnTopOfBox = true; // show character behind the ladder int backOrderLayer = OrderLayerObject.sortingOrder - cm.SORTING_ORDER_BEHIND_OFFSET; SetPlayerSortingOrder(player, backOrderLayer); } } } }
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, use for check last frame. /// </summary> /// <param name="collider"> Collider u want to draw. </param> /// <param name="col"> Color type. </param> /// <param name="origin">Provide the origin position. </param> public static void DrawCollider(BoxCollider2D collider, Color col, Vector3 origin) { #if (UNITY_EDITOR) // get width and height information. Vector2 boxInfo = JCS_Physics.GetColliderInfo(collider); Vector3 pos = origin; 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; } }
protected void OnTriggerStay(Collider other) { CharacterController cc = other.GetComponent <CharacterController>(); if (cc == null) { return; } bool isTopOfBox = JCS_Physics.TopOfBoxWithSlope(cc, mPlatformCollider); if (isTopOfBox) { Physics.IgnoreCollision( mPlatformCollider, cc, false); } else { Physics.IgnoreCollision( mPlatformCollider, cc, true); } JCS_2DSideScrollerPlayer p = other.GetComponent <JCS_2DSideScrollerPlayer>(); if (p == null) { return; } bool isJumpDown = p.IsDownJump(); if (!mCanBeDownJump) { // if cannot be down jump, fore it to false. isJumpDown = false; } if (p.CharacterState == JCS_2DCharacterState.CLIMBING || isJumpDown) { // IMPORTANT(JenChieh): Note that IgnoreCollision will reset // the trigger state of affected colliders, // so you might receive OnTriggerExit and // OnTriggerEnter messages in response to // calling this. Physics.IgnoreCollision( mPlatformCollider, p.GetCharacterController(), true); } if (isJumpDown && isTopOfBox) { if (JCS_PlatformSettings.instance != null) { /** * Make the player go down ward, so it will not stop by * the other collision detection. In order not to let the * render frame goes off. set the value as small as possible. */ p.VelY = -JCS_PlatformSettings.instance.POSITION_PLATFORM_DOWN_JUMP_FORCE; } else { JCS_Debug.Log( "No platform setting, could not set the down jump force..."); } } p.ResetingCollision = true; }