예제 #1
0
    public float RaycastVertical(float distance)
    {
        RaycastHit hit = new RaycastHit();

        Vector3 actualSize = new Vector3(selfCollider.size.x * selfTr.lossyScale.x, selfCollider.size.y * selfTr.lossyScale.y * skinWidthMultiplier, selfCollider.size.z * selfTr.lossyScale.z);

        if (Physics.BoxCast(selfTr.position + selfCollider.center, actualSize * 0.5f, Vector3.up * Mathf.Sign(distance), out hit, selfTr.rotation, Mathf.Abs(distance), checkMask))
        {
            float startPoint = selfTr.position.y + selfCollider.center.y + selfCollider.size.y * 0.5f * Mathf.Sign(distance);
            float newDistance = Mathf.Sign(distance) * Mathf.Abs(hit.point.y - startPoint);

            if (distance < 0) { flags.below = true; OnLanded?.Invoke(); }
            if (distance > 0) flags.above = true;

            lastVerticalHitResult = hit;

            return newDistance;
        }

        lastVerticalHitResult = hit;

        if (distance < 0) flags.below = false;
        if (distance > 0) flags.above = false;

        return distance;
    }
예제 #2
0
    /**
     * This coroutine waits for the moment where this object is supposed to connect to the ground after being thrown.
     */
    private IEnumerator FallStopper(float stopTime, Vector2 antiThrowImpulse, ForceEffect airResistanceEffect, [CanBeNull] ForceEffect gravityEffect)
    {
        yield return(new WaitForSeconds(stopTime));

        // Remove all forces which simulated the flow
        _physicsEffects.RemoveForce(airResistanceEffect);
        if (gravityEffect != null)
        {
            _physicsEffects.RemoveForce(gravityEffect);
        }
        // Cancel out the impulse which initiated the throw and prevent this object from sliding forever by reenabling
        // friction
        _physicsEffects.ApplyImpulse(antiThrowImpulse);
        _physicsEffects.FrictionEnabled = true;

        // restore sorting order & collision between the carrier and this object, as this object lands on the ground
        this.gameObject.layer = _cachedLayer;
        Physics2D.IgnoreCollision(_collider, _carrier.Collider, false);
        _renderer.sortingOrder--;

        foreach (var interactive in GetComponents <Interactive>())
        {
            interactive.enabled = true;
        }

        // While being thrown, we might loose contact to safe terrain, so we registered this behavior as terrain unsafe
        // for respawning.
        // Now that the throw has finished, we can unregister it again.
        if (TryGetComponent <Spawnable>(out var spawnable))
        {
            spawnable.UnregisterTouchingUnsafeTerrain(this);
        }

        OnLanded?.Invoke();
    }
예제 #3
0
 private void DetectLanding()
 {
     if (IsGrounded && !wasGroundedLastFrame)
     {
         OnLanded?.Invoke();
     }
     wasGroundedLastFrame = IsGrounded;
 }
    private Vector3 Collide(Vector3 velocity, Vector3 position, RaycastHit2D hit, ref float gravityForce)
    {
        // Vector3 finalVelocity = new Vector3(hit.point.x, hit.point.y) - position;

        Vector3 pushback;

        //if (Math.Abs(hit.point.y - hit.centroid.y) - hit.collider.bounds.size.y > float.Epsilon)
        //{
        //    pushback = new Vector3(0, hit.point.y - (position.y + velocity.y) + _lineThickness);
        //} else
        //{
        //    pushback = new Vector3(0, hit.point.x - (position.x + velocity.x) + _lineThickness);
        //}

        if (velocity.y < 0)
        {
            if (hit.collider.bounds.center.y + hit.collider.bounds.extents.y > position.y)
            {
                return(velocity);
            }
        }
        else
        {
            return(velocity);
        }

        pushback = new Vector3(0, hit.collider.bounds.center.y + hit.collider.bounds.extents.y - (position.y + velocity.y) + 0.001f);

        DebugHelp.PointPosition = new Vector3(hit.point.x, hit.point.y);

        Vector3 finalVelocity = velocity + pushback;

        //Vector3 finalVelocity = new Vector3(position.x, hit.collider.bounds.center.y + hit.collider.bounds.extents.y) - position;

        //Debug.Log(finalVelocity);

        if (finalVelocity.y > velocity.y && velocity.y < 0)
        {
            if (_isOnGround <= 0)
            {
                OnLanded?.Invoke(velocity);
            }
            _gravityForce = 0;
            _isOnGround   = 5;
            //Debug.Log("Ground");
        }

        return(finalVelocity);
    }
예제 #5
0
        private void CapsuleColliderHandler(CapsuleCollider collider)
        {
            IsGrounded = Physics.Raycast(
                collider.bounds.center,
                transform.Down(),
                collider.height / 2 + groundOffset
                );

            if (!previousState && IsGrounded)
            {
                OnLanded?.Invoke();
            }
            previousState = IsGrounded;


            if (debugMode)
            {
                Debug.DrawRay(
                    collider.bounds.center,
                    transform.Down() * (collider.height / 2 + groundOffset),
                    IsGrounded ? Color.green : Color.red
                    );
            }
        }
    private void Update()
    {
        if (playerHealth.isDead)
        {
            return;
        }
        UpdateCurrentTiles();
        UpdateJump();

        float currentInput = _moveInput;

        if (isGrounded && _groundedDuration < landedLockDuration)
        {
            if (_shouldLock)
            {
                currentInput = 0;
            }
        }
        else
        {
            _shouldLock = false;
        }

        if (currentInput != 0)
        {
            if (isGrounded)
            {
                _velocity.x = Mathf.MoveTowards(_velocity.x, speed * _moveInput, acceleration * Time.deltaTime);
            }
            else
            {
                _velocity.x = Mathf.MoveTowards(_velocity.x, aerialSpeed * _moveInput, acceleration * Time.deltaTime);
            }
        }
        else
        {
            _velocity.x = Mathf.MoveTowards(_velocity.x, 0, deceleration * Time.deltaTime);
        }



        if (velocity.y > 0)
        {
            _externalForce.y = 0;
        }
        _controller2D.move(_velocity * Time.deltaTime + _externalForce);

        _externalForce = Vector3.zero;
        if (!isGrounded)
        {
            _fallDuration    += Time.deltaTime;
            _groundedDuration = 0;
        }
        else
        {
            _groundedDuration += Time.deltaTime;
        }
        if (!_lastGrounded && isGrounded)
        {
            OnLanded?.Invoke(_fallDuration);
            if (_fallDuration > aerialTimeToLock)
            {
                _shouldLock = true;
            }
            _fallDuration = 0;
        }
        _lastGrounded = isGrounded;
    }
예제 #7
0
 public void EventLanded()
 {
     OnLanded?.Invoke();
 }