Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Adding several players to twitch chat integration unity

Discussion in 'Scripting' started by SinaBanana1, Oct 20, 2019.

  1. SinaBanana1

    SinaBanana1

    Joined:
    Oct 20, 2019
    Posts:
    8
    Hello!

    I am trying to add several players to twitch chat integration in unity so people writing in twitch can access different game objects by using different words. I'll attach the script. I'd be grateful for any hints or other resources. Thank you!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5. using System.ComponentModel;
    6. using System.Net.Sockets;
    7. using System.IO;
    8. using UnityEngine.UI;
    9. public class TwitchChat : MonoBehaviour
    10. {
    11.     private TcpClient twitchClient;
    12.     private StreamReader reader;
    13.     private StreamWriter writer;
    14.     public string username, password, channelName; //Get the password from https://twitchapps.com/tmi
    15.     public Text chatBox;
    16.     public Rigidbody player;
    17.     public Rigidbody player;
    18.     public int speed;
    19.     void Start()
    20.     {
    21.         Connect();
    22.     }
    23.     void Update()
    24.     {
    25.         if (!twitchClient.Connected)
    26.         {
    27.             Connect();
    28.         }
    29.         ReadChat();
    30.     }
    31.     private void Connect()
    32.     {
    33.         twitchClient = new TcpClient("irc.chat.twitch.tv", 6667);
    34.         reader = new StreamReader(twitchClient.GetStream());
    35.         writer = new StreamWriter(twitchClient.GetStream());
    36.         writer.WriteLine("PASS " + password);
    37.         writer.WriteLine("NICK " + username);
    38.         writer.WriteLine("USER " + username + " 8 * :" + username);
    39.         writer.WriteLine("JOIN #" + channelName);
    40.         writer.Flush();
    41.     }
    42.     private void ReadChat()
    43.     {
    44.         if (twitchClient.Available > 0)
    45.         {
    46.             var message = reader.ReadLine(); //Read in the current message
    47.             if (message.Contains("PRIVMSG"))
    48.             {
    49.                 //Get the users name by splitting it from the string
    50.                 var splitPoint = message.IndexOf("!", 1);
    51.                 var chatName = message.Substring(0, splitPoint);
    52.                 chatName = chatName.Substring(1);
    53.                 //Get the users message by splitting it from the string
    54.                 splitPoint = message.IndexOf(":", 1);
    55.                 message = message.Substring(splitPoint + 1);
    56.                 //print(String.Format("{0}: {1}", chatName, message));
    57.                 chatBox.text = chatBox.text + "\n" + String.Format("{0}: {1}", chatName, message);
    58.                 //Run the instructions to control the game!
    59.                 GameInputs(message);
    60.             }
    61.         }
    62.     }
    63.     private void GameInputs(string ChatInputs)
    64.     {
    65.         if (ChatInputs.ToLower() == "amanda")
    66.         {
    67.             player.AddForce(Vector3.left * (speed * 1000));
    68.         }
    69.         if (ChatInputs.ToLower() == "sina")
    70.         {
    71.             player.AddForce(Vector3.right * (speed * 1000));
    72.         }
    73.         if (ChatInputs.ToLower() == "jenny")
    74.         {
    75.             player.AddForce(Vector3.forward * (speed * 1000));
    76.         }
    77.     }
    78. }
    79.