Turning a light on or off by keyboard input

Dear all,

Let me start by saying I’m a neuroscientist in training, using Unity to create a virtaul reality to perform my experiment in. I’ve been recreating my labroom in Unity, and this has worked out well, with a lot of outside help. I now have an avatar sitting at a table. In front of the avatar are two little bulbs, with spotlights with sunflare in the same place. I want to be able to turn the lights on and off at will. The timing and measuring will be programmed in MatLab. So I need a way to let MatLab tell Unity to turn the lights on and off. My intial idea is to try and get Unity to turn both lights on and off at the pressing of a certain keyboard key, and then let MatLab spoof, giving a signal as if that key was pressed. The problem is I really don’t know where to start. I am largely unfamiliar with Unity, and I am really hoping one of you experienced users could help me get this small thing done. So, in summary, this is what I need;

  1. Explanation of how to be able to turn two lights on and off at will, independently of each other with a key press.

  2. A better way to turn two lights on and off at will, independently of each other, that enables me to use MatLab to tell unity what to do and when.

It’s important that I have control over exactly how long (how many frames) the lights are on. The effect I am measuring is in the order of 30-70 ms, so every frame can fuck up my results.

I’d be eternally great full to anyone who takes the time to help me in any way, however small.

Narameh

var light1:Light;
private var lightOn : boolean = false;

function Update()
{

if (Input.GetKeyDown ("o"))
{
lightOn = !lightOn;
light1.enabled = lightOn;
}
}

This will turn a light on and off. You can do the same code for the second light. Just drag your light object onto the light1 variable in the inspector. The script can be attached to any object you choose. I chose “o” as the key to press, but substitute whatever you like.:slight_smile:

Not sure I followed this:

I am assuming you mean you press one button one light switches on/off, you press another button the other light switches on/off.
If so, here’s a little something to get you started. Attach this script to your light to switch it on/off if a space bar is pressed.

function Update() {
  if (Input.GetKeyDown (KeyCode.Space)) {
      light.enabled = !light.enabled;
}
}

for your second question,
If you have unity pro you can use external dlls. Not sure what is available there, but I would imagine there would be some c++ matlab dlls that you could use.

Why do you need matlab? Perhaps you can create your scenario with matlab and then just replay it in unity or if the matlab functions you are using are not too complex, then recode them in unity alltogether.

EDIT: I seet tool55 beat me to it :slight_smile:

I would use a UDP / TCP connection between the matlab sdk based application and unity, that way they can be on different machines and whatever.

also plugins don’t get you much as they can’t call unity functions or disable stuff themself, all they can do is send messages which then call functions.

@tool55; Wonderful! It works exactly right at least in Unity. Haven’t tested the integration with MatLab as of yet, but I am very happy to be able to get this far.

@ivkoni; Your avatarpic is very, very disturbing :P. I unfortunately do not have access to unity pro. My supervisor wants me to work only within existing licences of the university open source software so his next students can pick up where I leave off. I need Matlab because I’m using a homemade tapperdevice to present tactile stimuli on the fingers of my participant during the experiment. It was built by someone who doesn’t work here anymore and didn’t leave instructions, and so I had to write a new driver for it. Since MatLab is the only programming language I really feel comfortable with, and I don’t have time within my 9 month project to learn an entire new one, that seemed my best bet.

Don’t get me wrong, I like programming, I get the logic, I understand the scripts you sent me and am able to tweak them to my needs, I just don’t have the “vocabulary” or experience in other languages to know the syntax and write complex things in them from scratch (or at least, not as of yet). I want to learn more eventually, just as a hobbyist nerd, but it takes too much time out of my project to try and rewrite everything in a largely unknown script.

@dreamora; I’ll look into that sometime later. I am first going to try if my idea even works, but I will definitely experiment with other options before I start doing the actual experiment.

Thank you all very, very much for answering and thinking with me. I know it must be frustrating to have someone ask for a quick solution like this, instead of starting from the bottom and learning my way up myself. Believe me, I’d love to do that if I had the time, but unfortunately I don’t. I hope I can count on as nice a response next time I have trouble figuring it out myself.

Narameh