Exemplo n.º 1
0
        /// <summary>
        /// 移動先の高さをボックスコライダーでチェックして、必要ならミニジャンプします。
        /// 移動していない時は、チェックしません。
        /// </summary>
        public void CheckMiniJump()
        {
            Vector3 move = Vector3.zero;

            move.x = Input.GetAxisRaw("Horizontal");
            if (Mathf.Approximately(move.x, 0f))
            {
                return;
            }

            // 移動先に段差がないかを確認
            float startOffset = ChrController.radius + boxColliderHalfExtents.x;

            checkCenter = ChrController.bounds.center
                          + move * startOffset;
            float dist     = (miniJumpCheckX - startOffset);
            int   hitCount = PhysicsCaster.BoxCast(checkCenter, boxColliderHalfExtents, move, dist, PhysicsCaster.MapCollisionPlayerOnlyLayer);

            if (hitCount == 0)
            {
                return;
            }

            float footh    = ChrController.bounds.min.y;
            int   hitIndex = -1;
            float h        = float.NegativeInfinity;

            for (int i = 0; i < hitCount; i++)
            {
                // ミニジャンプできないものは対象から外す
                Actable [] act = PhysicsCaster.hits[i].collider.GetComponents <Actable>();
                if (act != null)
                {
                    bool cantMiniJump = false;
                    for (int j = 0; j < act.Length; j++)
                    {
                        if (!act[j].CanMiniJump)
                        {
                            Log($"  cant MiniJump {PhysicsCaster.hits[i].collider.name}");
                            cantMiniJump = true;
                            break;
                        }
                    }
                    if (cantMiniJump)
                    {
                        continue;
                    }
                }

                float temph = PhysicsCaster.hits[i].collider.bounds.max.y - footh;
                if (temph > h)
                {
                    h        = temph;
                    hitIndex = i;
                }
            }

            if (hitIndex == -1)
            {
                return;
            }

            Log($"  h={h} > {ChrController.stepOffset} and <= {miniJumpHeight}");
            if ((h > ChrController.stepOffset) && (h <= miniJumpHeight))
            {
                targetJumpGround   = PhysicsCaster.hits[hitIndex].transform.position;
                targetJumpGround.y = ChrController.bounds.min.y + h;
                ChangeAction(ActionType.Jump);
            }
        }