Example #1
0
    // Use this for initialization
    void Start()
    {
        // make sure this object has a collider
        Collider cd = GetComponent <Collider>();

        if (cd == null)
        {
            gameObject.SetActive(false);
            return;
        }

        // check if the collider is triggered.
        if (cd.isTrigger == false)
        {
            cd.isTrigger = true;
        }

        // if band object is null, defaultly set it to its parent
        if (BindObject == null)
        {
            BindObject = transform.parent;
        }

        // If it has bind object, make sure that object has collider and rigidbody
        if (BindObject != null)
        {
            Collider  bcd = BindObject.GetComponent <Collider>();
            Rigidbody brb = BindObject.GetComponent <Rigidbody>();
            if (bcd == null || brb == null)
            {
                Debug.LogWarning("Object:" + BindObject.name + " is supposed to have collider and rigidbody to be grabbed.");
            }
        }

        // set materials issues.
        Renderer rdr = BindObject.GetComponent <Renderer> ();

        if (rdr != null)
        {
            originMat = rdr.material;
        }

        State            = GRABCOLLIDER_STATE.TO_ENTER;
        LeftHandFingerIn = 0;
    }
Example #2
0
 private void SwitchToReadyEnter()
 {
     State = GRABCOLLIDER_STATE.TO_ENTER;
     transform.localScale = transform.localScale / ExpandScale;
     // transform.localPosition -= ExpandOffset;
 }
Example #3
0
    private void SwitchToReadyExit(bool isLeftHand)
    {
        if (isLeftHand)
        {
            State = GRABCOLLIDER_STATE.TO_EXIT_LEFT;
        }
        else
        {
            State = GRABCOLLIDER_STATE.TO_EXIT_RIGHT;
        }

        ExpandOffset = Vector3.zero;

        // expand issue
        if (!AutomaticExpand)
        {
            transform.localScale = transform.localScale * ExpandScale;
        }
        else
        {
            // Automatic expand
            // Get Collider Type see if it's supported for automatically expand.
            // Final distance
            GameObject gobj;
            if (isLeftHand)
            {
                gobj = GameObject.Find("Hand_l");
            }
            else
            {
                gobj = GameObject.Find("Hand_r");
            }

            if (gobj == null)
            {
                return;
            }

            Transform t = gobj.transform.Find("palm");
            // Debug.Log("Grab:" + gobj);

            float    finalDistance = (t.position - transform.position).magnitude;
            Collider cd            = GetComponent <Collider>();
            if (cd is SphereCollider)
            {
                SphereCollider tmp = (SphereCollider)cd;
                ExpandScale = finalDistance / tmp.radius;
            }
            else if (cd is BoxCollider)
            {
                Ray          r  = new Ray(t.position, transform.position);
                RaycastHit[] rh = Physics.RaycastAll(r, finalDistance + 1.0f);
                foreach (RaycastHit h in rh)
                {
                    if (h.collider.name == gameObject.name)
                    {
                        ExpandScale = finalDistance / (finalDistance - h.distance);
                    }
                }
            }
            else if (cd is MeshCollider)
            {
                Vector3      centerPoint = cd.bounds.center;
                Ray          r           = new Ray(t.position, centerPoint);
                RaycastHit[] rh          = Physics.RaycastAll(r, finalDistance + 1.0f);
                foreach (RaycastHit h in rh)
                {
                    if (h.collider.name == gameObject.name)
                    {
                        ExpandScale  = finalDistance / (finalDistance - h.distance);
                        ExpandOffset = (centerPoint - transform.position) * (ExpandScale - 1.0f);
                    }
                }
            }
            else
            {
                Debug.LogWarning("You want to apply automatic collider expand to an unsupport collider");
            }

            ExpandScale = Mathf.Clamp(ExpandScale, 0.95f, 1.5f);

            transform.localScale = transform.localScale * ExpandScale;
            // transform.localPosition += ExpandOffset;
        }
    }