Пример #1
0
        protected virtual CollisionDirection GetCollisionAlongCurrentVelocity(LayerMask layer, float timeStep)
        {
            CollisionDirection directionSet = new CollisionDirection();

            Vector2 origin               = gameObject.transform.position;
            Vector2 direction            = body.velocity.normalized;
            float   distanceNextTimeStep = body.velocity.magnitude * timeStep;

            RaycastHit2D raycastHit2D = Physics2D.Raycast(origin, direction, distanceNextTimeStep, layer);

            //we're not going to hit anything
            if (raycastHit2D.collider == null)
            {
                return(directionSet);
            }

            Dev.Log("We're about to hit a wall!");
            float x  = gameObject.transform.position.x;
            float y  = gameObject.transform.position.y;
            float px = raycastHit2D.point.x;
            float py = raycastHit2D.point.y;

            float dx = Mathf.Abs(x - px);
            float dy = Mathf.Abs(y - py);

            //is it an x collision or a y collision?
            if (dx > dy)
            {
                //x collision, hitting left or right from us?
                if (px < x)
                {
                    directionSet.left = true;
                }
                else
                {
                    directionSet.right = true;
                }
            }
            else
            {
                if (py < y)
                {
                    directionSet.below = true;
                }
                else
                {
                    directionSet.above = true;
                }
            }

            return(directionSet);
        }
Пример #2
0
        }//end CheckTouching

        //if the values are within the tolerance, the object is not enough in that direction to be considered offset from us
        static protected CollisionDirection GetDirectionToTarget(GameObject self, GameObject target, float toleranceX = 0.1f, float toleranceY = 0.5f)
        {
            CollisionDirection direction = new CollisionDirection();
            float num  = self.transform.position.x;
            float num2 = self.transform.position.y;
            float num3 = target.transform.position.x;
            float num4 = target.transform.position.y;

            direction.right = (num < num3) && Mathf.Abs(num - num3) > toleranceX;
            direction.left  = (num > num3) && Mathf.Abs(num - num3) > toleranceX;
            direction.above = (num2 < num4) && Mathf.Abs(num2 - num4) > toleranceY;
            direction.below = (num2 > num4) && Mathf.Abs(num2 - num4) > toleranceY;

            return(direction);
        }