Connecting the LEGO EV3 Bot as gamecontroller

Hi,

I’m new to Unity. For a school project we need to make a game that that you need to control with a LEGO EV3 Robot.
I’ve got the code to connect and use the EV3 as a controller. But the problem is that I’ve got a Parsing error that I don’t understand:

PlayerControl.cs(5,14): error CS8025: Parsing Error

Our teachers don’t work with Unity and nobody on school is able to help me since nobody used Unity. Therefore I’m here to ask you the more experienced/ pro’s for help.

EDIT:
Code cleanup after replys

using UnityEngine;
using System.Collections;
using EV3MessengerLib;

// EV3: Create an EV3Messenger object which you can use to talk to the EV3.

//EV3Messenger ev3Messenger = new EV3Messenger();

// EV3: Connect to the EV3 serial port over Bluetooth.
//      If the program 'hangs' on a call to ev3Messenger.Connect, 
//      then your EV3 is not paired with your PC yet/anymore. 
//      To pair: Remove the EV3 from the Windows Bluetooth device list and add it again.
 // Hardcoded serial port: put the serial port 
// of the Bluetooth connection to your EV3 here!
public class PlayerControl : MonoBehaviour {
 public GameControlScript control;
 CharacterController controller;
 bool isGrounded= false;
 public float speed = 6.0f;
 public float jumpSpeed = 8.0f;
 public float gravity = 20.0f;
 private Vector3 moveDirection = Vector3.zero;
// EV3: The EV3Messenger is used to communicate with the Lego EV3
private EV3Messenger ev3Messenger;
 //start 
 void Start () {
  controller = GetComponent<CharacterController>();
        ev3Messenger = new EV3Messenger();
        ev3Messenger.Connect("COM03");
 }
    //// Game can be controlled by the connected EV3
    //UpdateUsingEV3();


 // Update is called once per frame
 void Update (){
  if (controller.isGrounded) {
   animation.Play("run");            //play "run" animation if spacebar is not pressed
   moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);  //get keyboard input to move in the horizontal direction
   moveDirection = transform.TransformDirection(moveDirection);  //apply this direction to the character
   moveDirection *= speed;            //increase the speed of the movement by the factor "speed"
       
            if (ev3Messenger.IsConnected)
            {
                // EV3: Receive a new command from mailbox "COMMAND" of the EV3
                // and use it to change the direction of the paddle or to exit the game.
                EV3Message message = ev3Messenger.ReadMessage();
                if (message != null
                   && message.MailboxTitle == "Command")
                {
                    if (message.ValueAsText == "Right")
                    {
                        transform.position += transform.right * speed; 
                    }
                    else if (message.ValueAsText == "Left")
                    {
                        transform.position += -transform.right * speed; 
                    }
                    else if (message.ValueAsText == "Exit")
                    {
                        ev3Messenger.Disconnect();
                        //Exit();
                    }
                }
            }
     
   //if (Input.GetButton ("Jump")) {          //play "Jump" animation if character is grounded and spacebar is pressed
    //animation.Stop("run");
    //animation.Play("jump_pose");
    //moveDirection.y = jumpSpeed;         //add the jump height to the character
   //}
   if(controller.isGrounded)           //set the flag isGrounded to true if character is grounded
    isGrounded = true;
  }
  moveDirection.y -= gravity * Time.deltaTime;       //Apply gravity  
  controller.Move(moveDirection * Time.deltaTime);      //Move the controller
 }
 //check if the character collects the powerups or the snags
 void OnTriggerEnter(Collider other)
 {              
  if(other.gameObject.name == "Powerup(Clone)")
  {
   control.PowerupCollected();
            //EV3 sends Message MakeNoise so it will play the sound Hit
            ev3Messenger.SendMessage("MakeNoise", "Hit");
  }
  else if(other.gameObject.name == "Obstacle(Clone)" && isGrounded == true)
  {
   control.AlcoholCollected();
            //EV3 sends Message MakeNoise so it will play the sound Crash
            ev3Messenger.SendMessage("MakeMoreNoise", "Crash");
  }
   
  Destroy(other.gameObject);
   
 }
}
}

I can explain things if the code if not clear enough.

Thank you in advance for looking into it!

Line 5 needs a variable type, and possibly can’t initialise outside of a function.
So you need line 5 to read
EV3Messenger ev3Messenger;
to create the typed variable, then inside start;
ev3Messenger = new EV3Messenger();
to populate it

Thank you for your reply. And you where right. I cleaned/shifted the code.

Only problem now is that I’ve got a Internal compiler error that gives me this message:

Internal compiler error. See the console log for more information. output was:
Unhandled Exception: System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.
at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (bool)
at System.Reflection.Assembly.GetTypes () [0x00000] in :0
at Mono.CSharp.RootNamespace.ComputeNamespaces (System.Reflection.Assembly assembly, System.Type extensionType) [0x00000] in :0

Well that is as you can see very unclear. If you got a few minutes is it possible to check my project? http://athena.fhict.nl/users/i322877/Temple_run.zip

Thnak you very much for the help so far!

What’s this EV3Messenger class you’re using? From the error, I’m guessing that it’s not included in the built app. Where’d you get it, and are you sure it’s compatible with Unity?

The class is used to create the function to send a Message in the EV3 Robot. The robot has a program with instructions that it needs to do when EV3Messenger is called.

I changed the code above to the one I’ve now.

I figured that much, but you haven’t answered the important questions. Where’d you get it, and are you sure it’s compatible with Unity?

I got the EV3 code from school. We only needed to implement it correctly.
But I heard that Unity uses Net 2.0 and EV3 uses NET 4.0. So if that is true then it’s not possible to connect it??

Yes, that could be possible. That’s why it’s important whether the library has been verified for use with Unity. If not, this may not be something you can fix in a reasonable amount of time. :frowning:

Well In the passed other students where able to connect Unity with the EV3 Robot. Only these students are nowhere to be find. Maybe there is a short term solution? Any you know?

Thanks for your help so far!

The original source can be found here: https://ev3messenger.codeplex.com/
Would be great if someone can get this to work with Unity.

Welcome to the world of pioneers and early adopters. A quick google search brings up nothing. So you are on your own unless you can find someone who has solved this before.

It’s worth questioning why you want to do this. If you have to ask about parsing errors you are not a competent coder. (At least in the Unity environment). If the teacher is not familiar with unity then it shouldn’t be an absolute requirement. You’ve picked a hard topic that will require an intimate knowledge of Unity, C# and ev3 to solve. It’s doable, but will take you a long time.

The original library/source at codeplex uses SerialPort (.NET 4) and ConcurrentQueue(.NET 4.5/4.6). All other stuff should be Unity compatible I think/hope.

I looked at the plugin documentation Unity - Manual: Plug-ins
Tried the managed plugin example. I had to select .NET 3.5 to get the ultra simple plugin to work so I guess you need to find a replacement for SerialPort and ConcurrentQueue:
-I found a Unity compatible ConcurrentQueue at Concurrent Queue for Unity · GitHub. Only TryDequeue method, which is used by the EV3 source, is missing. Don’t know how difficult it’s to implement this yourself.
-For SerialPort you could use an third party non-free DLL: Serial Port Development Component for Windows | ActiveXperts Software
Then you have to rewrite all original SerialPort references to use this new third party component.

Other option is to leave the EV3 sources alone, and build some functionality on top of it to support a connection from Unity for example sockets.

If EV3 messenger program and Unity runs on the same machine you could look into inter-process communication (IPC). There are many IPC methods (pipes, files, sockets…) and the right choice depends on your specific requirements. For node.js IPC, see for instance javascript - What's the most efficient node.js inter-process communication library/method? - Stack Overflow

If you read javascript - What's the most efficient node.js inter-process communication library/method? - Stack Overflow
you will notice ZeroMQ which can superfast send/receive messages (from Unity to EV3 connection program) and is now .NET 3.5 compatible: .NET 3.5 compatibility · Issue #98 · zeromq/netmq · GitHub
To support bidirectional messaging you could make both a server and client in both Unity and EV3 messenger.

2 Likes