Esempio n. 1
0
        /// <summary>
        /// On fixedUpdate, checks the last movement and if needed casts a ray to detect obstacles
        /// </summary>
        protected virtual void Update()
        {
            _lastMovement        = this.transform.position - _positionLastFrame;
            _lastMovementSquared = _lastMovement.sqrMagnitude;

            // if we've moved further than our bounds, we may have missed something
            if (_lastMovementSquared > _squaredBoundsWidth)
            {
                float movementMagnitude = Mathf.Sqrt(_lastMovementSquared);

                // we cast a ray backwards to see if we should have hit something
                RaycastHit2D hitInfo = MMDebug.RayCast(_positionLastFrame, _lastMovement.normalized, movementMagnitude, ObstaclesLayerMask, Color.blue, true);

                if (hitInfo.collider != null)
                {
                    if (hitInfo.collider.isTrigger)
                    {
                        hitInfo.collider.SendMessage("OnTriggerEnter2D", _collider, SendMessageOptions.DontRequireReceiver);
                    }

                    if (!hitInfo.collider.isTrigger)
                    {
                        Hit = hitInfo;
                        this.gameObject.SendMessage("PreventedCollision2D", Hit, SendMessageOptions.DontRequireReceiver);
                        if (RepositionRigidbody)
                        {
                            this.transform.position = hitInfo.point - (_lastMovement / movementMagnitude) * _adjustedSmallestBoundsWidth;
                            _rigidbody.position     = hitInfo.point - (_lastMovement / movementMagnitude) * _adjustedSmallestBoundsWidth;
                        }
                    }
                }
            }
            _positionLastFrame = this.transform.position;
        }