0

preface: i am very new to c#, and this is an object grabbing script i have taken from a prefab and edited.

script goal: detect if a game object is in pickup distance. if it is, when the grip trigger is pressed, parent the object to the virtual hand. when the trigger is released, remove the parent relationship. Also, while the object is parented to the controller, if the B button is held, scale the object up, and if the A button is held, scale the object down, and reset the scale upon thumbstick click.

I can see when my controller collides with the game object, but when i press the designated button to parent the object, nothing seems to happen.

I'd really appreciate any help I could get with this script, as there might be a significant amount i'm doing wrong.

I should mention that I don't really need any sort of physics simulations, all i need is the ability to manipulate an object floating in space for viewing purposes.

Thanks in advance!

Here is my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Grabber : MonoBehaviour
{

    // Update is called once per frame
    void Update()
    {
        OVRInput.Update();

        if ((OVRInput.Get(OVRInput.Axis1D.SecondaryHandTrigger)) > 0.2f && CollidingObject)
        {
            GrabObject();
        }
        if ((OVRInput.Get(OVRInput.Axis1D.SecondaryHandTrigger)) < 0.2f && ObjectInHand)
        {
            ReleaseObject();
        }
        if (OVRInput.Get(OVRInput.Button.Two) && ObjectInHand)
        {
            ScaleUp();
        }
        if (OVRInput.Get(OVRInput.Button.One) && ObjectInHand)
        {
            ScaleDown();
        }
        if (OVRInput.Get(OVRInput.Button.SecondaryThumbstick) && ObjectInHand)
        {
            ScaleReset();
        }
    }


    public GameObject CollidingObject;
    public GameObject ObjectInHand;

    public void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.GetComponent<Rigidbody>())
        {
            CollidingObject = other.gameObject;
        }
    }

    public void OnTriggerExit(Collider other)
    {
        CollidingObject = null;
    }   

    private void GrabObject()
    {
        ObjectInHand = CollidingObject;
        ObjectInHand.transform.SetParent(this.transform);
        ObjectInHand.GetComponent<Rigidbody>().isKinematic = true;
    }

    private void ReleaseObject()
    {
        ObjectInHand.GetComponent<Rigidbody>().isKinematic = false;
        ObjectInHand.transform.SetParent(null);
        ObjectInHand = null;
    }

    Vector3 scaleChangeUp = new Vector3(0.01f, 0.01f, 0.01f);
    Vector3 scaleChangeDown = new Vector3(-0.01f, -0.01f, -0.01f);
    public void ScaleUp()
    {
        ObjectInHand.transform.localScale += scaleChangeUp;
    }

    public void ScaleDown()
    {
        ObjectInHand.transform.localScale += scaleChangeDown;
    }

    private void ScaleReset()
    {
        ObjectInHand.transform.localScale = Vector3.one;
    }

}
CG415
  • 1
  • 1
  • 1
    Don't parent Rigidbodies! Rather use a [`FixedJoint`](https://docs.unity3d.com/Manual/class-FixedJoint.html). Also a kinematic Rigidbody still needs to be moved only in `FixedUpdate`! Maybe you should rather enable and disable the entire Rigidbody component – derHugo Feb 24 '20 at 19:47
  • Also you should ignore the trigger enters while you already hold an object .. otherwise you might grab a new object as soon as you collide with it while holding grab down ;) – derHugo Feb 24 '20 at 19:53
  • Thank you for the advice @derHugo ! I should mention that I don't really need any sort of physics simulations, all i need is the ability to manipulate an object floating in space for viewing purposes. – CG415 Feb 24 '20 at 20:00
  • Have you confirmed in the inspector that the object is not set to parent? – TheBatman Feb 24 '20 at 20:31
  • @TheBatman Yeah, it looks like they are not parenting in the inspector. the collision detection works, however. Here is a screenshot of both my inspector on the hands as well as the settings of my grabbable cube. https://imgur.com/a/RDLURsG – CG415 Feb 24 '20 at 20:42

0 Answers0