Search Unity

Input behaviour using Events through code

Discussion in 'Scripting' started by V-J, Sep 26, 2019.

  1. V-J

    V-J

    Joined:
    Apr 1, 2015
    Posts:
    73
    I'm developing for the quest and in my scene I have two cubes that I can select with the laserpointer. I have used the OnPointerDown Event, but it responds on input from both controllers the thumbsticks and buttons (four & one)

    How can i specify the input from only the SecondaryIndexTrigger in the OnPointerDown Event?

    Code (CSharp):
    1. public void OnPointerDown(PointerEventData eventData)
    2.     {
    3.         selectedObject = eventData.pointerCurrentRaycast.gameObject;
    4.         HighLiteGo();
    5.     }
     
    Last edited: Sep 28, 2019
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
  3. V-J

    V-J

    Joined:
    Apr 1, 2015
    Posts:
    73
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
  5. V-J

    V-J

    Joined:
    Apr 1, 2015
    Posts:
    73
    @palex-nx Sorry for asking the question the wrong way i think, i will try to explain this in the best way possible.

    I have two cubes is my scene with boxcolliders attached. And i have a OVR Physics Raycaster on the OVRCameraRig. Also i have a UIHelper prefab with the laser pointer in the scene.

    When i press the right index trigger on the right controller it's preforming a raycast, when it is detecting a gameobject i can interact with it. all good so far. But the problem is, when i aim my rightcontroller on the gameobject without pressing any button or trigger on the right controller, and use the left controller thumbstick or index trigger, it also selecting the gameobject (cube).

    How can i prevent that when pressing the trigger or using the thumbstick on the left controller it detects the gameobject?
     
    Last edited: Sep 30, 2019
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Maybe I get something wrong. In the pointer click handler, you have an instance of PointerEventData. Is the .button value in that object different for different controllers? I believe it should but may be not. In case if not, the new input system I referenced allows you to make difference between controllers sending the events. So far, I believe one of those thing will help you to create what you want to. If no, feel free to as more.
     
  7. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Or may be it's .pointerId?
     
  8. V-J

    V-J

    Joined:
    Apr 1, 2015
    Posts:
    73
    @palex-nx If i understand your answer correctly, yes it is different, i get only the left, middle and right. but i need to get the
    Axis1D.SecondaryHandTrigger

    https://developer.oculus.com/documentation/unity/latest/concepts/unity-ovrinput/

    I'm using OnPointerDown event, maybe i need to use another way to get hold of the hit object of the raycast from the OVRPhysicsRaycaster script.

    P.s. i'm not that good in coding yet, so i maybe completely wrong here..
     
    Last edited: Sep 30, 2019
  9. V-J

    V-J

    Joined:
    Apr 1, 2015
    Posts:
    73
    Could something like this be a solution?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3. using UnityEngine.EventSystems;
    4. using UnityEngine.UI;
    5. using System;
    6. [AddComponentMenu("UI/MyButton")]
    7. public class MyButton : Button
    8. {
    9.      UnityEvent mOnDown = new UnityEvent();
    10.      UnityEvent mOnUp = new UnityEvent();
    11.      public override void OnPointerDown(PointerEventData eventData)
    12.      {
    13.          base.OnPointerDown(eventData);
    14.          mOnDown.Invoke();
    15.      }
    16.      public override void OnPointerUp(PointerEventData eventData)
    17.      {
    18.          base.OnPointerUp(eventData);
    19.          mOnUp.Invoke();
    20.      }
    21.      public UnityEvent onDown
    22.      {
    23.          get{ return mOnDown; }
    24.      }
    25.    
    26.      public UnityEvent onUp
    27.      {
    28.          get{ return mOnUp; }
    29.      }
    30. }
     
    Last edited: Sep 30, 2019