예제 #1
0
    public void SecondGrapple()
    {
        //For raycasting from character to mouse position
        Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        int          layerMask = ~(1 << player.gameObject.layer);
        RaycastHit2D hit       = Physics2D.Raycast(player.transform.position, new Vector2(worldPoint.x - player.transform.position.x, worldPoint.y - player.transform.position.y), 10.0f, layerMask);

        //Display if its a good or bad raycast
        if (hit.collider != null)
        {
            Debug.DrawRay(player.transform.position, new Vector3(worldPoint.x - player.transform.position.x, worldPoint.y - player.transform.position.y, 0), Color.green);
        }
        else
        {
            Debug.DrawRay(player.transform.position, new Vector3(worldPoint.x - player.transform.position.x, worldPoint.y - player.transform.position.y, 0), Color.red);
        }

        var allTags = hit.collider.gameObject.GetComponent <CustomTag>();

        if (allTags != null && allTags.HasTag("CanGrab") && player.Stats.OnHook == false)
        {
            if (hit.collider.GetComponent <Grabbable>().hookInstalled == false)
            {
                //Make sure multiple objects arent instantiated
                hit.collider.GetComponent <Grabbable>().hookInstalled = true;
                player.Components.Target2 = hit.collider.gameObject;

                //Attach a reference in each game object to their connected object
                player.Components.Target1.GetComponent <Grabbable>().connectedObject = player.Components.Target2;
                player.Components.Target2.GetComponent <Grabbable>().connectedObject = player.Components.Target1;

                //Instantiate rope
                Gear.GenerateRope(player.Components.Target1.transform, player.Components.Target2, 6);
            }
        }
    }