示例#1
0
    private void Update()
    {
        HandleMovement();

        m_PointedItem?.Highlight(false);
        m_PointedItem = null;

        Ray        ray = new Ray(m_CameraTransform.position, m_CameraTransform.forward);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, m_GrabDistance, m_GrabableMask))
        {
            m_PointedItem = hit.collider.GetComponentInParent <Grabable>();
            if (m_PointedItem)
            {
                m_PointedItem.Highlight(true);
            }
        }

        if (Input.GetMouseButtonDown(0) && m_GrabbedItem)
        {
            var gun = m_GrabbedItem.GetComponent <Gun>();
            if (gun)
            {
                gun.Fire();
            }
        }

        if (Input.GetMouseButtonDown(0) && m_PointedItem)
        {
            m_GrabbedItem = m_PointedItem;
            m_GrabbedItem.Highlight(false);
            m_GrabbedItem.Grab(m_GrabHook);
        }

        if (Input.GetMouseButtonDown(1) && m_GrabbedItem)
        {
            m_GrabbedItem.Release();
            m_GrabbedItem = null;
        }

        if (Input.GetKeyDown(KeyCode.E) && hit.collider)
        {
            //Debug.Log(hit.collider.gameObject.name);
            var button = hit.collider.GetComponentInParent <BigButton>();
            if (button)
            {
                button.Push();
            }
        }
    }
示例#2
0
    // Find all Grabable objects and Intersect them
    // Uses "Grabable" tag for selection purposes. This is probably
    // a good place to start profiling for performance improvements.
    void TestAllGrabablesForIntersection()
    {
        GameObject[] grabableObjects   = GameObject.FindGameObjectsWithTag("Grabable");
        Vector3?     intersectionPoint = null;

        Grabable closestGrabable     = null;
        float    closestGrabDistance = float.MaxValue;

        foreach (GameObject grabableObject in grabableObjects)
        {
            Grabable grabable = grabableObject.GetComponent <Grabable>();
            if (grabable == null)
            {
                Debug.Log("Object with tag 'grabable' " + grabableObject.name + " does not have a Grabable component");
                continue;
            }
            intersectionPoint = IntersectsPath(grabableObject.GetComponent <Collider>());
            if (intersectionPoint != null && DistanceToPoint(intersectionPoint.Value) < closestGrabDistance)
            {
                closestGrabDistance = DistanceToPoint(intersectionPoint.Value);
                closestGrabable     = grabable;
            }
        }

        // Short circuit if nothing is changing.
        if (currentPointee == closestGrabable)
        {
            return;
        }

        // If we're no longer pointing to our currentPointee, stop highlighting.
        if (currentPointee != null)
        {
            currentPointee.Unhighlight(this);
            currentPointee = null;
        }

        // If we found anything, highlight it.
        if (closestGrabable != null)
        {
            closestGrabable.Highlight(this);
            currentPointee       = closestGrabable;
            currentPointeeHandle = intersectionPoint.Value;
        }
    }