예제 #1
0
    private void OnCollisionEnter2D(Collision2D other)
    {
        Debug.Log(this.gameObject.name);

        if (other.gameObject.tag == GlobalVariables.TETHER_TAG)
        {
            VerletRope tether = other.gameObject.GetComponent <VerletRope>();
            tether.TryCutRope(other.contacts[0].point);
        }
    }
예제 #2
0
    // Can't really do this because the parent rigidbody of the fish means we're always going
    // to see the parent object, not the child, so we can't be sure we're hitting the spear.
    // private void OnCollisionEnter2D(Collision2D other) {
    //     Debug.Log(other.otherCollider.gameObject.tag);
    //     if (other.gameObject.tag == GlobalVariables.FISH_TAG && cuttable) {
    //         if (cuttable) {
    //             CutRope(other.GetContact(0).point);
    //         }
    //     }
    // }

    public void TryCutRope(Vector2 position)
    {
        // Create two new ropes. They'll each have their nozzle position set but no target.
        // The number of segments to create will depend on the position at which the rope was cut.

        if (!cuttable)
        {
            return;
        }

        int segmentsFromTop = 0;

        for (int i = 0; i < ropeSegments.Count - 1; i++)
        {
            if (ropeSegments[i].posNow.y > position.y && ropeSegments[i + 1].posNow.y < position.y)
            {
                segmentsFromTop = i;
                break;
            }
        }

        VerletRope newRopeTop = GameObject.Instantiate(tetherPrefab).GetComponent <VerletRope>();

        newRopeTop.ropeLength        = segmentsFromTop * segmentLength;
        newRopeTop.cutRopeParticles  = cutRopeParticles;
        newRopeTop.segmentCount      = segmentsFromTop;
        newRopeTop.averageRopeTarget = null;
        newRopeTop.cuttable          = false;
        newRopeTop.nozzle            = nozzle;
        newRopeTop.target            = null;

        VerletRope newRopeBottom = GameObject.Instantiate(tetherPrefab).GetComponent <VerletRope>();

        newRopeBottom.ropeLength        = ropeLength - (segmentsFromTop * segmentLength);
        newRopeBottom.segmentCount      = segmentCount - segmentsFromTop;
        newRopeBottom.cutRopeParticles  = cutRopeParticles;
        newRopeBottom.averageRopeTarget = null;
        newRopeBottom.cuttable          = false;
        newRopeBottom.nozzle            = null;
        newRopeBottom.target            = target;

        GameManager.instance.KillPlayer();
        GameObject.Destroy(this.gameObject);
    }
예제 #3
0
    // Start is called before the first frame update
    void Start()
    {
        Material ropeMaterial = new Material(Shader.Find("Unlit/Color"));

        GameObject gun   = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
        Rigidbody  gunRb = gun.AddComponent <Rigidbody>();

        gunRb.isKinematic = true;
        Destroy(gun.GetComponent <Collider>());
        gun.transform.position = transform.position + 0.25f * transform.forward + 0.25f * transform.right;
        gun.transform.rotation = transform.rotation;
        gun.transform.parent   = transform;
        VerletRope verletRope = gun.AddComponent <VerletRope>();

        //VerletRope verletRope = gameObject.AddComponent<VerletRope>();
        verletRope.BuildRope(physicsAnchor, 6, maxRopeLength, ropeMaterial);
        verletRope.Spring = 5000.0f;
        verletRope.Damper = 1000.0f;
    }
예제 #4
0
    IEnumerator LaunchHookCoroutine()
    {
        ropeRenderer.enabled       = true;
        ropeRenderer.positionCount = 2;

        while (Vector3.Distance(transform.position, hook.transform.position) < maxRopeLength)
        {
            Collider[] colliders         = new Collider[1];
            LayerMask  hookableLayerMask = LayerMask.GetMask("HookableLayer");
            LayerMask  ballLayerMask     = LayerMask.GetMask("Ball");
            if (Physics.OverlapSphereNonAlloc(hook.transform.position, 0.1f, colliders, hookableLayerMask | ballLayerMask) > 0)
            {
                //ropeRenderer.enabled = false;
                if (colliders[0].tag == "Hookable" || colliders[0].tag == "BounceBall")
                {
                    hook.GetComponent <Rigidbody>().isKinematic = true;
                    hook.transform.parent = colliders[0].transform;

                    isGrappled = true;

                    verletRope = gameObject.AddComponent <VerletRope>();
                    verletRope.BuildRope(hook.transform, 6, maxRopeLength, ropeMaterial);
                    verletRope.Spring = 5000.0f;
                    verletRope.Damper = 1000.0f;
                    state             = HookState.Attached;
                }
                else
                {
                    state = HookState.Retracting;
                    StartCoroutine(RetractHookCoroutine());
                }
                yield break;
            }
            ropeRenderer.positionCount = 2;
            ropeRenderer.SetPositions(new Vector3[] { transform.position, hook.transform.position });
            yield return(null);
        }
        state = HookState.Retracting;
        //ropeRenderer.enabled = false;
        StartCoroutine(RetractHookCoroutine());
        yield return(null);
    }