Beispiel #1
0
        void WebControl(KeyCode key, LegData leg)
        {
            if (Input.GetKeyDown(key))
            {
                if (!leg.spring)
                {
                    GameObject legObject    = leg.gameObject;
                    Transform  legTransform = legObject.transform;
                    LayerMask  layerMask    = ~(1 << LayerMask.NameToLayer("Player"));
                    // shoot web from the tip of the leg
                    Vector2      legEnd  = legTransform.position + (legTransform.up * leg.height);
                    RaycastHit2D hitInfo = Physics2D.Raycast(legEnd, legTransform.up, WebSwingRange, layerMask);
                    if (hitInfo.collider)
                    {
                        if (hitInfo.collider.tag == "Enemy")
                        {
                            DamageInfo dmgSource = new DamageInfo(Damage,
                                                                  Position,
                                                                  hitInfo.collider.gameObject.transform.position,
                                                                  leg.PlayerNumber,
                                                                  hitInfo.collider);
                            hitInfo.rigidbody.SendMessage("Die", dmgSource);
                        }

                        // if out leg is inside the wall don't create spring
                        if (!legEnd.Equals(hitInfo.point))
                        {
                            leg.webHitInfo = hitInfo;
                            SpringJoint2D spring = legObject.AddComponent <SpringJoint2D> ();
                            spring.autoConfigureDistance = false;
                            spring.distance     = 0;
                            spring.dampingRatio = 0.9F;
                            spring.frequency    = 0.3F;
                            // it should be up because joint start should be attached to the tip of the leg, but due to
                            // hingejoint bugs its atm connected to the body
                            spring.anchor          = Vector2.zero;
                            spring.connectedAnchor = hitInfo.point;
                            leg.spring             = spring;
                            leg.swingCollision     = true;
                            leg.lr.SetPosition(1, leg.spring.connectedAnchor);
                            leg.lr.enabled = true;
                        }
                    }
                    else
                    {
                        // did not hit anything, change web color to red with the end at the maximum range
                        leg.swingCollision = false;
                        Vector2 reachedPoint = legTransform.position + legTransform.up * (leg.height + WebSwingRange);
                        leg.lr.SetPosition(1, reachedPoint);
                        leg.lr.enabled = true;
                    }

                    AudioUtils.Play("Laser");
                }
            }
            else if (Input.GetKeyUp(key))
            {
                leg.lr.enabled = false;
                SpringJoint2D spring = leg.spring;
                if (spring)
                {
                    Destroy(spring);
                    leg.webHitInfo = new RaycastHit2D();
                }
            }

            if (leg.lr.enabled)
            {
                if (leg.webHitInfo.collider && !leg.webHitInfo.collider.name.ToLower().Contains("enemy"))   // if we're connected to (not enemy)
                // shoot ray again to check if the string should be destroyed
                {
                    LayerMask    layerMask    = ~(1 << LayerMask.NameToLayer("Player"));
                    Transform    legTransform = leg.gameObject.transform;
                    Vector2      legEnd       = legTransform.position + (legTransform.up * leg.height);
                    RaycastHit2D hitInfo      = Physics2D.Raycast(legEnd, leg.webHitInfo.point - legEnd, WebSwingRange, layerMask);
                    if (Vector2.SqrMagnitude(hitInfo.point - leg.webHitInfo.point) > 0.001F)
                    {
                        leg.lr.enabled = false;
                        SpringJoint2D spring = leg.spring;
                        Destroy(spring);
                        leg.webHitInfo = new RaycastHit2D();

                        AudioUtils.Play("FabricTorn", hitInfo.point);
                        return;
                    }
                }

                Vector2 legTip = leg.gameObject.transform.position + Quaternion.Euler(0, 0, leg.gameObject.transform.eulerAngles.z) * new Vector2(0, leg.height);
                if (leg.swingCollision)
                {
                    leg.lr.SetPosition(0, legTip);
                    leg.lr.SetColors(Color.white, Color.white);
                }
                else
                {
                    leg.lr.SetColors(Color.red, Color.red);
                    leg.lr.SetPosition(0, legTip);
                }
            }
        }
Beispiel #2
0
 public static GameObject SplatFromDamageInfo(DamageInfo info)
 {
     return(SplatFromDirection(info.TargetPosition, info.TargetPosition - info.SourcePosition, info.Damage));
 }