element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino Projects
  • Products
  • Arduino
  • Arduino Projects
  • More
  • Cancel
Arduino Projects
Blog Real World Effects while 3D Gaming with Arduino
  • Blog
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino Projects to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Sean_Miller
  • Date Created: 14 Aug 2018 2:47 AM Date Created
  • Views 744 views
  • Likes 7 likes
  • Comments 0 comments
  • unity
  • gaming
  • arduino
Related
Recommended

Real World Effects while 3D Gaming with Arduino

Sean_Miller
Sean_Miller
14 Aug 2018

A bucket list item for me was getting a 3D video game published.  Although I have a ton of web apps in use where I have worked to eliminate paper forms and processes, its been since the 1990's that I published software globally.  But that was freely distributable on aminet.net for the amiga and after 22 years has only received 1500 downloads.  The 2nd version - much improved - only got 89!  Ouch.

 

Amazingly, Aminet.net still exists and looks the same!  Here's a trip down memory lane for those of you who may remember ACE BASIC by David Benn for the Amiga in the 90's.  ACE was a pretty big deal because you could write applications that looked like they were written in C, but write them in BASIC - for free!  I recently reconnected with David Benn again - he's down under with Dave Jones.  image

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

Since then, I evolved through C, Java, Java3D, C#, javascript, a Game Editor called DXStudio, and then discovered Unity at Unity3D.com.  They give away their game engine!  Digging around, I found that you could tweak it to send serial to the COM ports.  That's when it hit me - I can talk to an Arduino!  (or any other microcontroller).

 

So, that gave me the idea to make my game have a hidden feature to allow for "4D Gaming".  When you hit the "m" key during the pause menu, it will loop through each COM port so you can select your microcontroller.  From there, you can have your Arduino do all kinds of crazy things in the physical world like:

 

  • Smart Lights turn on, off, or cycle.
  • A knocking sound suddenly comes from another room.
  • Small vibrators clipped at strategic locations on your clothes to your shirt suddenly give you jump.
  • Water is misted out to your face.
  • A radio turns on to static or playing spooky sounds in another room.
  • A fan turns on and blows air at you.
  • Turn on one of the electric pulse zapper things they give people with chronic back pain.

 

So, you need code on both sides - the Unity Gaming Engine and the Arduino.

 

Here is a very basic script for Unity that you can attach to the default light source when opening a new Unity Project:

using UnityEngine;
using System.IO.Ports;
public class SeanSerialTesterScript : MonoBehaviour {
    SerialPort sp;
    float next_time; int ii = 0;
    // Use this for initialization
    void Start () {
        string the_com="";
        next_time = Time.time;
        
        foreach (string mysps in SerialPort.GetPortNames())
        {
            print(mysps);
            if (mysps != "COM1") { the_com = mysps; break; }
        }
        sp = new SerialPort("\\\\.\\" + the_com, 9600);
        if (!sp.IsOpen)
        {
            print("Opening " + the_com + ", baud 9600");
            sp.Open();
            sp.ReadTimeout = 100;
            sp.Handshake = Handshake.None;
            if (sp.IsOpen) { print("Open"); }
        }
    }
    // Update is called once per frame
    void Update() {
        if (Time.time > next_time) { 
            if (!sp.IsOpen)
            {
                sp.Open();
                print("opened sp");
            }
            if (sp.IsOpen)
            {
                print("Writing " + ii);
                sp.Write((ii.ToString()));
            }
            next_time = Time.time + 5;
            if (++ii > 9) ii = 0;
        }
    }
}

This outputs a number from 0 to 9 to a COM Port.

 

To catch the serial on the Arduino, you would do this code:

int inByte;
void setup() {
  // start serial port at 9600 bps:
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT); 
  digitalWrite(LED_BUILTIN, LOW);
}
void loop() {
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();
    if ((57-inByte)==0)
    { //after pressing m in pause screen and connected
      blink_it (10);
    }

    if ((57-inByte)==1) 
    {
      //Saw kid    
      blink_it(1);
    }
    if ((57-inByte)==2) 
    {
      //Lightening/Thunder
      blink_it(2);
    }
    if ((57-inByte)==3) 
    {
      //Spooky Noises
      blink_it(3);
    }
    if ((57-inByte)==4) 
    {
      //Sudden sound like the door being beat on
      blink_it(4);
    }
    if ((57-inByte)==5) 
    {
      //Chilling Ambient Sound
      blink_it(5);
    }
    if ((57-inByte)==6) 
    {
      //door creak
       blink_it(6);
    }
    if ((57-inByte)==7) 
    {
      //voices talking
       blink_it(7);
    }
    if ((57-inByte)==8) 
    {
      //drone woke kid
       blink_it(8);
    }
    if ((57-inByte)==9) 
    {
      //next chapter
       blink_it(9);
    }
  }
  delay(500);
}

void blink_it(int the_count){
  for (int ii=0;ii<the_count;ii++){
     digitalWrite(LED_BUILTIN, HIGH);
     delay(150);
     digitalWrite(LED_BUILTIN, LOW);
     delay(150);
  }        
}

 

 

Instead of doing the function "blink_it", you can replace it with code that drives pins to then hit transistors, relays, and even do web client requests if you have a WiFI shield or MKR1000.  This allows you to couple your Unity Game Engine Code to the Internet of Things - the sky is the limit!

 

In my game, its a jump scare game similar to Five Nights and Freddy's, but with a very original story.  It's still in Early Access, but is playable all the way through and the Maker Mode is enabled.  The comments in the code link the game events to when that serial command will trigger.  So, for example, you can have super bright LEDs flash behind you when Lightening/Thunder hits when receiving the serial command 2.

 

Here's the link to the early release game:

https://store.steampowered.com/app/905120/SonLightSleepwalker/

 

If you check it out, don't let him get into the light!

 

If you want to make your own game starting with the code above, go to Unity3d.com and download the Personal Edition. Feel free to comment here for tips to get started.

 

-Sean

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube