watsonx Assistant

 View Only
Expand all | Collapse all

How can I integrate Watson Assistant with Unity 3D

  • 1.  How can I integrate Watson Assistant with Unity 3D

    Posted Mon July 17, 2023 09:36 AM

    How can I integrate Watson Assistant with Unity 3D to provide an AI-powered game where the movement of the game charactors can be controlled via voice commands from IBM Watson STT?

    Can you provide example code which will send a web hook to Visual studio?

    Thanks in advance,

    Darren



    ------------------------------
    Darren Bartholomew
    ------------------------------


  • 2.  RE: How can I integrate Watson Assistant with Unity 3D

    IBM Champion
    Posted Wed July 26, 2023 01:53 PM

    As I believe this a sophisticated endeavor, it requires a sophisticated answer. So i'll provide extensive details for your question.
    Please note that all code and samples are for explanation purposes only.
    To achieve this, we'll use Watson Speech-to-Text (STT) to recognize voice commands, and then we'll control the game characters' movement based on the recognized commands. To send a webhook from Unity to Visual Studio, we'll use Unity's built-in WWW class to make HTTP requests. Below is a step-by-step guide along with example code to accomplish this integration.

    Prerequisites:

    1. IBM Cloud account with access to Watson Assistant and Speech-to-Text services.
    2. Unity 3D installed on your development machine.

    Step 1: Create a Watson Assistant Chatbot and Obtain Credentials

    1. Sign in to your IBM Cloud account and create a new Watson Assistant service instance.

    2. Create a new Watson Assistant workspace and design the conversation flow with relevant intents, entities, and responses.

    3. Obtain the API key and Assistant ID for your Watson Assistant service.

    Step 2: Set Up Watson Speech-to-Text Service

    1. Create a new Speech-to-Text service instance on IBM Cloud.

    2. Obtain the API key and service URL for your Speech-to-Text service.

    Step 3: Create a Unity 3D Project

    1. Open Unity 3D and create a new 3D project.

    Step 4: Import Watson Unity SDK

    1. Import the IBM Watson Unity SDK into your Unity project. You can download the SDK from the IBM Watson Developer Cloud GitHub repository: https://github.com/IBM/unity-sdk-core.

    2. Follow the installation instructions provided in the SDK's README file to set up the SDK in your Unity project.

    Step 5: Set Up Voice Control Script

    Here's an example Unity C# script to handle voice commands and control the game characters' movement:

    using UnityEngine; using UnityEngine.Networking; using IBM.Watson.SpeechToText.V1; using IBM.Watson.Assistant.V2; using IBM.Cloud.SDK.Utilities; using IBM.Cloud.SDK.Authentication.Iam; public class VoiceControl : MonoBehaviour { private SpeechToTextService speechToText; private AssistantService assistant; private string watsonSTT_APIKey = "YOUR_STT_API_KEY"; private string watsonSTT_URL = "YOUR_STT_SERVICE_URL"; private string watsonAssistant_APIKey = "YOUR_ASSISTANT_API_KEY"; private string watsonAssistant_URL = "YOUR_ASSISTANT_SERVICE_URL"; private string assistantId = "YOUR_ASSISTANT_ID"; private string session_id; void Start() { // Set up Watson Speech-to-Text service IamAuthenticator speechToTextAuthenticator = new IamAuthenticator(apikey: watsonSTT_APIKey); speechToText = new SpeechToTextService(speechToTextAuthenticator); speechToText.SetServiceUrl(watsonSTT_URL); // Set up Watson Assistant service IamAuthenticator assistantAuthenticator = new IamAuthenticator(apikey: watsonAssistant_APIKey); assistant = new AssistantService(versionDate: "2021-06-14", authenticator: assistantAuthenticator); assistant.SetServiceUrl(watsonAssistant_URL); // Create a new session with the Watson Assistant assistant.CreateSession(OnCreateSession, assistantId); } private void OnCreateSession(DetailedResponse<SessionResponse> response, IBMError error) { if (error == null) { session_id = response.Result.SessionId; Debug.Log("Session ID: " + session_id); } } void Update() { if (Input.GetKeyDown(KeyCode.Space)) { // Start recording voice input when Space key is pressed speechToText.Recognize(OnRecognize, watsonSTT_URL, true, true); } } private void OnRecognize(SpeechRecognitionEvent result, Dictionary<string, object> customData) { if (result != null && result.results.Length > 0) { string command = result.results[0].alternatives[0].transcript; Debug.Log("Command: " + command); // Call Watson Assistant to process the command assistant.Message(OnMessage, assistantId, session_id, input: new MessageInput() { Text = command }); } } private void OnMessage(DetailedResponse<MessageResponse> response, IBMError error) { if (error == null) { string responseText = response.Result.Output.Generic[0].Text; Debug.Log("Response: " + responseText); // TODO: Implement character movement based on the response from Watson Assistant } } }

    Step 6: Implement Character Movement

    In the OnMessage method of the script above, you will receive the response from Watson Assistant. Depending on the response, you can implement character movement in your game accordingly. This may involve updating the position and rotation of the game characters or triggering specific animations.

    Step 7: Test the Integration

    Run your Unity 3D project, and when you press the Space key, the script will start recording your voice input. After speaking a command, the script will send the recorded audio to Watson Speech-to-Text, and then Watson Assistant will process the recognized text. The response from Watson Assistant will be logged in the Unity console. You can further implement character movement logic based on the received response.

    Please note that this example script is a basic starting point for integrating IBM Watson Assistant with Unity 3D. Depending on your specific game and requirements, you can customize the script and implement more sophisticated interactions and game mechanics.



    ------------------------------
    Youssef Sbai Idrissi
    Software Engineer
    ------------------------------



  • 3.  RE: How can I integrate Watson Assistant with Unity 3D

    Posted Thu July 27, 2023 03:15 PM

    Hello Youssef,

    Can you confirm whether the Watson Unity SDK  is still functioning or available for use? From my research, IBM had already put the SDK out of service which is why I posed this question of a solution of one might allow Unity and IBM Watson to communicate with each other?

    Or perhaps I have misunderstood something?

    I look forward to hearing from you.

    Kind regards,

    Darren



    ------------------------------
    Darren Bartholomew
    ------------------------------