示例#1
0
    private void Play()
    {
        rigidbodies.ForEach(x => x.isKinematic = false);
        joints.ForEach(x => x.enabled          = true);
        var rigidbodiesInCar = carRigidbody.GetComponentsInChildren <Rigidbody2D>();

        foreach (var r in rigidbodiesInCar)
        {
            r.isKinematic = false;
        }
        carRigidbody.isKinematic = false;
    }
示例#2
0
    // For higher performance assumes that slowFractions contains rb
    protected bool SlowDown(Rigidbody2D rb, float frac)
    {
        if (rb == null)
        {
            return(false);
        }
        SlowData slowData = slowFractions[rb];

        if (slowData.isInside && slowData.slowFrac > slowFraction)
        {
            float frac_ = Mathf.Min(frac, slowData.slowFrac - slowFraction);
            rb.velocity        *= 1 - frac_ / slowData.slowFrac;
            rb.angularVelocity *= 1 - frac_ / slowData.slowFrac;

            slowData.slowFrac -= frac_;

            foreach (var keeper in rb.GetComponents <SlowKeeper>())
            {
                keeper.slowFactor = slowData.slowFrac;
            }
            foreach (var keeper in rb.GetComponentsInChildren <SlowKeeper>())
            {
                keeper.slowFactor = slowData.slowFrac;
            }

            return(true);
        }
        return(slowData.isInside);
    }
示例#3
0
    static public float ComputeInertia(Rigidbody2D body)
    {
        //CALCULATE INERTIA, TAKING SCALING AND ALL THE CHILD COLLIDERS INTO ACCOUNT
        float   hypothetic_mass    = 0f;
        float   hypothetic_density = 1f;
        float   I      = 0f;
        Vector2 center = Vector2.zero;

        Collider2D[] colliders = body.GetComponentsInChildren <Collider2D>();

        for (int i = 0; i < colliders.Length; ++i)
        {
            Collider2D c = colliders[i];
            //PICK ONLY OWN COMPS AND ADDITIONAL CHILD COLLIDERS THAT USE SAME BODY
            if (c.gameObject == body.gameObject || (c.transform.parent.gameObject == body.gameObject && c.rigidbody2D == null))
            {
                MassData data = null;

                CircleCollider2D circle = c as CircleCollider2D;
                if (circle != null)
                {
                    data = CircleMassInertiaCenter(circle, hypothetic_density, body.transform.position);
                }

                BoxCollider2D box = c as BoxCollider2D;
                if (box != null)
                {
                    data = BoxMassInertiaCenter(box, hypothetic_density, body.transform.position);
                }

                PolygonCollider2D poly = c as PolygonCollider2D;
                if (poly != null)
                {
                    data = PolygonMassInertiaCenter(poly, hypothetic_density, body.transform.position);
                }

                if (data != null)
                {
                    hypothetic_mass += data.mass;
                    center          += data.center * data.mass;
                    I += data.inertia;
                }
            }
        }

        //PORTED FROM BOX2D
        // Compute the center of mass.
        center *= 1f / hypothetic_mass;

        // Center the inertia about the center of mass
        I -= hypothetic_mass * center.sqrMagnitude;
        //SCALE IT
        I *= body.mass / hypothetic_mass;

        return(I);
    }
示例#4
0
        public void StopMovement()
        {
            Rigidbody2D body = GetComponent <Rigidbody2D>();

            foreach (Rigidbody2D childBody in body.GetComponentsInChildren <Rigidbody2D>())
            {
                childBody.velocity        = Vector3.zero;
                childBody.angularVelocity = 0.0f;
            }
        }
示例#5
0
    public void RecalculateBalance()
    {
        Colliders = top.GetComponentsInChildren <Collider2D>();
        balance   = 0f;
        for (int i = 0; i < Colliders.Length; i++)
        {
            if (Colliders[i].CompareTag("Top"))
            {
                continue;
            }

            float distance = transform.position.x - Colliders[i].transform.position.x;
            balance += distance;
        }

        balance /= Colliders.Length;
        balance *= 0.2f;
        balance  = Mathf.Clamp(balance, -1f, 1f);
    }
示例#6
0
    public void HoldBlock()
    {
        Rigidbody2D RBC = currentBlock.GetComponent <Rigidbody2D>();

        RBC.gravityScale = 0f;
        RBC.velocity     = new Vector2();
        RBC.bodyType     = RigidbodyType2D.Static;
        Collider2D [] C2 = RBC.GetComponentsInChildren <Collider2D>();
        for (int i = 0; i < C2.Length; i++)
        {
            C2[i].isTrigger = false;
        }
        isHook = true;
        Ora.GetComponent <OraControl>().blockfalling = false;
        Ora.SetActive(false);

        button.sprite = hookspr;
        VJ.SetActive(true);
    }
示例#7
0
    public void Update()
    {
        curPos = transform.position;
        Rigidbody2D[] rbs = rb.GetComponentsInChildren <Rigidbody2D>();

        if (script.connectedObject != null)
        {
            Debug.Log("Trying to fly");
            rb.AddForce(new Vector2(0, 1.0f));
        }
        else
        {
            Debug.Log("Falling");
            rb.angularDrag = 20.0f;
        }
        if (curPos.y < startPos.y)
        {
            rb.velocity = new Vector2(0, 0.5f);
        }
    }
示例#8
0
    void Mount(GameObject obj)
    {
        riderBody          = obj.GetComponent <Rigidbody2D>();
        riderBody.bodyType = RigidbodyType2D.Kinematic;

        movement = obj.GetComponent <PlayerMovement>();
        movement.rotateToDirection = false;
        movement.moveToDirection   = false;
        movement.rideController    = root.GetComponent <RideCthuluController>();

        var mountColliders = root.GetComponentsInChildren <Collider2D>(true);
        var riderColliders = riderBody.GetComponentsInChildren <Collider2D>(true);

        foreach (var itMount in mountColliders)
        {
            foreach (var itRider in riderColliders)
            {
                Physics2D.IgnoreCollision(itMount, itRider);
            }
        }
    }
示例#9
0
 void Start()
 {
     car   = GetComponent <Rigidbody2D>();
     wheel = car.GetComponentsInChildren <Collider2D>();
     anim  = GetComponent <Animator>();
 }