Pick up Objects using Google Daydream Controller
Hi Everyone,
I'm stuck with the Google Daydream Controller. I want to pick up an object when I press the AppButton. I tried several tutorials (https://www.youtube.com/watch?v=gllyvcrB69I) but it doesn't work. I can't find a tut for Daydream. Although i look at the Gvr references, i get confused what classes and functions to use to make it work.
I can get the rotation the same as the controller and i thought parenting the object to the Daydream controller would work, but it's not...
Can somebody explain how to pick something up using the Daydream Controller?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ClickChangeColor : MonoBehaviour {
GameObject otherGameObject;
//Vector3 v = GvrController.Orientation * Vector3.forward;
// Use this for initialization
void Start () {
otherGameObject = GameObject.FindWithTag ("Pickup");
}
// Update is called once per frame
void Update () {
if (GvrController.AppButton) {
gameObject.transform.localRotation = GvrController.Orientation;
//gameObject.transform.SetParent(Transform );
gameObject.transform.parent = otherGameObject.transform;
}
}
}
What is "otherGameObject"? Is that a reference to GvrControllerPointer?
that is the object i'm trying to 'pickup'
The thing is i can't figure out WHERE and HOW I can shoot a raycast from the daydream controller. With Cardboard e.g. you just 'shoot' a raycast from the middle of the screen through your camera, but in Daydream i need to shoot a raycast from the controller, not the camera but i can't figure out which classes i have to reference....
Is the raycast being made in 'GvrLaserPointer' or in GvrLaserPointerImpl? in 'GvrController' maybe? The documentation doesn't help...
I know you've already discovered this, but to help future people:
The plugin integrates with Unity's Event System for raycasting. https://docs.unity3d.com/$$anonymous$$anual/EventSystem.html
This includes custom Input $$anonymous$$odules and Custom Raycasters. GvrPointerInput$$anonymous$$odule GvrPointerGraphicRaycaster GvrPointerPhysicsRaycaster
The raycasting is done by the raycasters, and you can respond to events via EventTrigger of IEventSystemHandlers. https://docs.unity3d.com/$$anonymous$$anual/SupportedEvents.html
$$anonymous$$ore detail can be found at https://developers.google.com/vr/unity/guide under the section "User Interaction".
If you have a use case where you want to do your own raycasting you can access the transform that the raycast should originate from like this: GvrPointer$$anonymous$$anager.Pointer.PointerTransform. If you are using the GvrControllerPointer prefab, the PointerTransform refers to the laser. If you are using GvrReticlePointer, the PointerTransform refers to the reticle.
An example of how this is setup can be found in the scene GvrDemo within the plugin.
Answer by fiixed · Mar 23, 2017 at 03:14 AM
Hey @sanderbos24,
I found this tutorial really helpful. Its written by a Google dev and he uses raycasts from the controller for teleportation.
http://www.sdkboy.com/2016/12/building-daydream-controller-based-app-scratch/
In this shorter one, he shows how to get the raycast without using an event on the target:
http://www.sdkboy.com/2017/03/daydream-vr-raycast-data-gvrbasepointer-without-eventtrigger/
Hope this helps.
oh thanks a lot! The first one i did indeed, but it's a 'closed' system, don't know what's going on in the 'EventTrigger' of the object. The second one i didn't see yet! If i understand correctly you attach this script to the 'player', but you still need an GvrEventSystem right?
Yes, you attach the script to the player.
You need a GVREventSystem in the Hierarchy (from the prefab in the SD$$anonymous$$), but thats it.
You can all implement EventSystemHandlers ins$$anonymous$$d of using Event Trigger. https://docs.unity3d.com/ScriptReference/EventSystems.IPointerClickHandler.html
Answer by sanderbos24 · Mar 23, 2017 at 04:27 AM
Now I got the teleportation to work i'm trying to pick stuff up. I'm copying this tutorial (https://www.youtube.com/watch?v=runW-mf1UH0) (min. 7:45) and replace it with the Daydream components. It seems to have problems with the 'IsKinematic' function in void carry.
i attached this script to the 'player' and the 'Pickupable' script to an object i want to pick up.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickupObject : MonoBehaviour {
GameObject mainController;
bool carrying;
GameObject carriedObject;
public float distance;
// Use this for initialization
void Start () {
mainController = GameObject.FindWithTag ("MainCamera");
}
// Update is called once per frame
void Update () {
if (carrying) {
carry (carriedObject);
} else {
pickup ();
}
}
void carry(GameObject o) {
o.rigidbody.IsKinematic = true;
o.transform.position = mainController.transform.position + mainController.transform.forward * distance;
}
void pickup ()
{
if (GvrController.AppButton) {
GvrLaserPointerImpl laserPointerImpl = (GvrLaserPointerImpl)GvrPointerManager.Pointer;
if (laserPointerImpl.IsPointerIntersecting) {
Pickupable p = GetComponent<Pickupable> ();
if (p != null) {
carrying = true;
carriedObject = p.gameObject;
}
}
}
}
}
'Pickupable script' (nothing) in it yet)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pickupable : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
I feel like i'm almost there, but i'm missing something.. :P
Ins$$anonymous$$d of using the camera as the mainController, you probably want the pickupable object to follow the daydream controller. Try using GvrPointer$$anonymous$$anager.Pointer.PointerTransform to access it.
GvrPointer$$anonymous$$anager.Pointer.PointerTransform is the transform used to create the raycast when you use GvrEventSystem and GvrPointerPhysicsRaycaster. If you use GvrControllerPointer for daydream, then the pointer is the laser. If you use GvrReticlePointer for cardboard, then the pointer is the gaze based reticle at the center of the screen. The scene GvrDemo should show how this is all setup.
Your answer
Follow this Question
Related Questions
Camera Follow Script destroys Raycast on VR-Hands with Late Update 1 Answer
[Daydream][C#]How would I make a simple directional controller with Google Daydream Controller? 1 Answer
GoogleVR / C# - How to, with the "pointer click": click 1 move a cube and click 2 move back the cube 0 Answers
Get Direction From Raycast 0 Answers