Example #1
0
    public void updateChild()
    {
        if (child == null)
        {
            return;
        }

        //rotate to face parent
        Vector2 point = (Vector2)transform.position;

        Quaternion rot = Quaternion.LookRotation(child.transform.position - new Vector3(point.x, point.y, -100f), Vector3.forward);

        child.transform.rotation    = rot;
        child.transform.eulerAngles = new Vector3(0f, 0f, transform.eulerAngles.z);

        //lock distance within trail distance
        Vector2 dPos = child.transform.position - transform.position;

        if (dPos.magnitude > trailDistance)
        {
            child.transform.position = transform.position + (Vector3)(dPos.normalized * trailDistance);
        }

        //go down the chain
        WormSegment segment = child.GetComponent <WormSegment> ();

        if (segment != null)
        {
            segment.updateChild();
        }
    }
    private void GrowLength()
    {
        WormSegment current = this.GetComponent <WormHead> ().Behind;

        while (current.Behind != null)
        {
            current = current.Behind;
        }
        current.ExtendWorm(1, 2);
    }
 private void GrowWidth()
 {
     if (this.gameObject.transform.localScale.magnitude < new Vector3(rateOfGrowth, rateOfGrowth, rateOfGrowth).magnitude)
     {
         this.gameObject.transform.localScale *= widthGrowth;
         WormSegment current = this.GetComponent <WormHead> ().Behind;
         while (current.Behind != null)
         {
             current.Behind.gameObject.transform.localScale *= widthGrowth;
             current = current.Behind;
         }
     }
 }
    public void ExtendWorm(int currentSize, int maxSize)
    {
        if (currentSize >= maxSize)
        {
            this.transform.localScale /= 2;            // tail will look half the size
            return;
        }
        WormSegment newSegment = Instantiate(wormSegmentPrefab, transform.parent);

        newSegment.name = "worm-segment-" + currentSize;
        newSegment.SetSegmentAhead(this);
        segmentBehind = newSegment;
        if (!segmentAhead)
        {
            newSegment.transform.localPosition = 2 * wormSpread * (transform.position - Vector3.up).normalized;
        }
        else
        {
            newSegment.transform.localPosition = wormSpread * (transform.position - segmentAhead.transform.position).normalized;
            newSegment.transform.localScale    = -segmentAhead.transform.localScale;          // each segment is a flip of the previous
        }
        newSegment.ExtendWorm(currentSize + 1, maxSize);
    }
 public void Die()
 {
     Behind = null;
     onDeath.Invoke();
     Destroy(this.gameObject, 5f);
 }
 public void SetSegmentAhead(WormSegment segment)
 {
     segmentAhead = segment;
 }