void OnTriggerEnter2D(Collider2D other) { Pushable pushable = other.GetComponent <Pushable>(); if (pushable != null) { m_CurrentPushables.Add(pushable); } }
void OnTriggerExit2D(Collider2D other) { Pushable pushable = other.GetComponent <Pushable>(); if (pushable != null) { if (m_CurrentPushables.Contains(pushable)) { m_CurrentPushables.Remove(pushable); } } }
public void CheckForPushing() { bool pushableOnCorrectSide = false; Pushable previousPushable = m_CurrentPushable; m_CurrentPushable = null; if (m_CurrentPushables.Count > 0 && CrossPlatformInputManager.GetAxis("Vertical") < 0.7f) { /* kk comment change * bool movingRight = PlayerInput.Instance.Horizontal.Value > float.Epsilon; * bool movingLeft = PlayerInput.Instance.Horizontal.Value < -float.Epsilon; */ //kk added change bool movingRight = CrossPlatformInputManager.GetAxis("Horizontal") > float.Epsilon; bool movingLeft = CrossPlatformInputManager.GetAxis("Horizontal") < -float.Epsilon; for (int i = 0; i < m_CurrentPushables.Count; i++) { float pushablePosX = m_CurrentPushables[i].pushablePosition.position.x; float playerPosX = m_Transform.position.x; if (pushablePosX < playerPosX && movingLeft || pushablePosX > playerPosX && movingRight) { pushableOnCorrectSide = true; m_CurrentPushable = m_CurrentPushables[i]; break; } } if (pushableOnCorrectSide) { Vector2 moveToPosition = movingRight ? m_CurrentPushable.playerPushingRightPosition.position : m_CurrentPushable.playerPushingLeftPosition.position; moveToPosition.y = m_CharacterController2D.Rigidbody2D.position.y; m_CharacterController2D.Teleport(moveToPosition); } } if (previousPushable != null && m_CurrentPushable != previousPushable) {//we changed pushable (or don't have one anymore), stop the old one sound previousPushable.EndPushing(); } m_Animator.SetBool(m_HashPushingPara, pushableOnCorrectSide); }
/* *推动物体逻辑概要: * 在状态机中所有状态类调用CheckForPushing()检测是否符合条件,如果是 * 设置m_CurrentPushable字段为当前推动的物体 * 判断可推动对象 相对角色的左右方向 及 键盘或者控制器的左右输入方向 是否要处理推动(如箱子在左,角色在右,此时输入为向右移动则不需要推动) * 执行m_CharacterController2D.Teleport(moveToPosition); * 注:CheckForPushing() 只判断角色是否符合推动状态条件,并设置Pushing flag,等待状态机的切换,及保持推动过程中角色和可推动对象的相对位置恒定 * 不做相关移动操作,而具体的移动逻辑为: * Pushing状态中调用m_MonoBehaviour.GroundedHorizontalMovement(true, m_MonoBehaviour.pushingSpeedProportion); 设置m_MoveVector(缓慢) * Pushing状态中调用MovePushable(),m_CurrentPushable.Move(m_MoveVector * Time.deltaTime); ***此处是以m_MoveVector为箱子移动速度 */ /// <summary> /// 判断是否处于推动状态 /// </summary> public void CheckForPushing() { bool pushableOnCorrectSide = false; Pushable previousPushable = m_CurrentPushable; m_CurrentPushable = null; if (m_CurrentPushables.Count > 0) { bool movingRight = PlayerInput.Instance.Horizontal.Value > float.Epsilon; bool movingLeft = PlayerInput.Instance.Horizontal.Value < -float.Epsilon; for (int i = 0; i < m_CurrentPushables.Count; i++) { float pushablePosX = m_CurrentPushables[i].pushablePosition.position.x; float playerPosX = m_Transform.position.x; if (pushablePosX < playerPosX && movingLeft || pushablePosX > playerPosX && movingRight) { pushableOnCorrectSide = true; m_CurrentPushable = m_CurrentPushables[i]; break; } } if (pushableOnCorrectSide) { Vector2 moveToPosition = movingRight ? m_CurrentPushable.playerPushingRightPosition.position : m_CurrentPushable.playerPushingLeftPosition.position; moveToPosition.y = m_CharacterController2D.Rigidbody2D.position.y; //m_CharacterController2D.transform.position == m_CharacterController2D.Rigidbody2D.position //2018.11.28 Hotkang 注: 此处的逻辑是:保持推动时角色和推动物体的相对位置不变(由pushable组件下的left和right position确定) //如果不调用该函数,会出现碰撞抖动:角色一直向前推动,但可推动对象已经不可向前移动(碰撞体接触),此时有可能推动对象会嵌入其他碰撞体 m_CharacterController2D.Teleport(moveToPosition); } } if (previousPushable != null && m_CurrentPushable != previousPushable) {//we changed pushable (or don't have one anymore), stop the old one sound previousPushable.EndPushing(); } m_Animator.SetBool(m_HashPushingPara, pushableOnCorrectSide); }
public void CheckForPushing() { var pushableOnCorrectSide = false; var previousPushable = m_CurrentPushable; m_CurrentPushable = null; if (m_CurrentPushables.Count > 0) { var movingRight = PlayerInput.Instance.Horizontal.Value > float.Epsilon; var movingLeft = PlayerInput.Instance.Horizontal.Value < -float.Epsilon; for (var i = 0; i < m_CurrentPushables.Count; i++) { var pushablePosX = m_CurrentPushables[i].pushablePosition.position.x; var playerPosX = m_Transform.position.x; if (pushablePosX < playerPosX && movingLeft || pushablePosX > playerPosX && movingRight) { pushableOnCorrectSide = true; m_CurrentPushable = m_CurrentPushables[i]; break; } } if (pushableOnCorrectSide) { Vector2 moveToPosition = movingRight ? m_CurrentPushable.playerPushingRightPosition.position : m_CurrentPushable.playerPushingLeftPosition.position; moveToPosition.y = m_CharacterController2D.Rigidbody2D.position.y; m_CharacterController2D.Teleport(moveToPosition); } } if (previousPushable != null && m_CurrentPushable != previousPushable) { previousPushable.EndPushing(); } m_Animator.SetBool(m_HashPushingPara, pushableOnCorrectSide); }