1 year ago
#386221
Suhas Rao
How to use a same Input action to trigger different events when used on different objects in UNITY?
I'm facing a small problem with the unity's New Input system.
I'm trying to build a VR game that has floating buttons in the scene. I have used a gaze reticle from Google VR. I'm using a joystick for input in VR mode. Below is the snapshot of my Input actions with bindings with the joystick.
As you can see I have an Action called Mouse_Button which is bound to key Y on my joystick (Don't mind about the name "LaserPrinter" unity detects the joystick by this name).
Below is the snapshot of my scene. Notice the buttons Input 1 and Input 2. These buttons both get triggered at the same time once I press the button on my joystick as shown.
But I want to trigger a particular button at which the user is looking at in VR mode.
Below is the c# script I have attached to the buttons .
// format for sending button state to blynk
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using UnityEngine.InputSystem;
public class Button_click : MonoBehaviour
{
PlayerControls controls;
public Button myButton;
public Text myText;
private string url_off;
private string url_on;
int cnt=0;
void Awake(){
controls = new PlayerControls();
controls.Gameplay.Mouse_Button.performed += ctx => OnButtonClicked();
// controls.Gameplay.Move_forward.canceled += ctx => Move;
}
public void OnButtonClicked()
{
// Debug.Log(cnt);
if(myButton.name == "Input 1"){
url_off = //some url;
url_on = //some url;
}
else{
url_off = //some url;
url_on = //some url;
}
if(myText.text.Contains("ON")){
Debug.Log("Button Clicked!");
myText.text = myButton.name + "\nOFF!";
StartCoroutine(GetRequest(url_off));
cnt = 1;
}
else{
Debug.Log("Button Clicked!");
myText.text = myButton.name + "\nON";
StartCoroutine(GetRequest(url_on));
cnt = 0;
}
}
void OnEnable()
{
controls.Gameplay.Enable();
}
void OnDisable()
{
controls.Gameplay.Disable();
}
void Start()
{
// myButton.onClick.AddListener(OnButtonClicked);
// // // if (BuyText == null)
// // BuyText = BuyButton.GetComponentInChildren<Text>();
}
IEnumerator GetRequest(string uri)
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
{
// Request and wait for the desired page.
yield return webRequest.SendWebRequest();
}
}
}
I have created two empty game objects and attached the script to the objects. For the buttons I have used Event triggers as shown.
Please help me find a way to trigger only a particular button at which the user is looking at.
I was not able to find any solution online. If the question is repeated, Please let me know.
c#
unity-game-engine
virtual-reality
0 Answers
Your Answer