Beispiel #1
0
        /// <summary>
        /// Updates the shadow's size and position at every frame
        /// </summary>
        protected virtual void Update()
        {
            // vertical raycast below the agent
            _hit = CorgiTools.CorgiRayCast(transform.position, Vector2.down, _rayLength, GroundMask, true, Color.red);

            if (_hit)
            {
                _shadow.GetComponent <Renderer>().enabled = true;
                // updates the shadow's position according to the hit
                _shadow.transform.position  = new Vector2(_hit.point.x, _hit.point.y);
                _shadow.transform.position += ShadowOffset;

                // handles horizontal offset based on the ground/object distance.
                float distance = _shadow.transform.position.y - transform.position.y + GetComponent <Renderer>().bounds.size.y / 2;
                _shadowOffsetBasedOnHeight  = (ShadowMaxHorizontalDistance / (Mathf.Abs(MaximumVerticalDistance / distance))) * Vector3.right;
                _shadow.transform.position += _shadowOffsetBasedOnHeight;

                // prevents the shadow from rotating
                _shadow.transform.rotation = Quaternion.identity;
                // updates the size of the shadow based on the distance between the agent and the ground
                _shadow.transform.localScale = _initialScale / (_initialScale.x + Mathf.Abs(distance / 2));
            }
            else
            {
                // if the raycast didn't hit anything (the object is not above solid ground), we just hide the shadow
                _shadow.GetComponent <Renderer>().enabled = false;
            }
        }
Beispiel #2
0
 /// <summary>
 /// Creates the initial shadow
 /// </summary>
 protected virtual void Initialize()
 {
     // we throw a raycast below the character and position the shadow accordingly
     _hit    = CorgiTools.CorgiRayCast(transform.position, Vector2.down, _rayLength, GroundMask, true, Color.red);
     _shadow = (GameObject)Instantiate(ShadowPrefab, _hit.point, Quaternion.identity);
     _shadow.transform.parent = this.transform;
     _initialScale            = _shadow.transform.localScale;
 }
Beispiel #3
0
        // On enable, we cast rays above and below the object to check for obstacles
        protected virtual void OnEnable()
        {
            RaycastHit2D raycastUpwards   = CorgiTools.CorgiRayCast(transform.position, Vector2.up, RaycastLength, 1 << LayerMask.NameToLayer("Ground"), true, Color.gray);
            RaycastHit2D raycastDownwards = CorgiTools.CorgiRayCast(transform.position, Vector2.up, RaycastLength, 1 << LayerMask.NameToLayer("Ground"), true, Color.gray);

            // if we see an obstacle, we reposition the object
            if (raycastUpwards)
            {
                Reposition(raycastUpwards.collider.transform.position);
            }
            if (raycastDownwards)
            {
                Reposition(raycastDownwards.collider.transform.position);
            }
        }