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

Unity Arduino control

Discussion in 'General Discussion' started by ZAlavender, Mar 30, 2021.

  1. ZAlavender

    ZAlavender

    Joined:
    Mar 30, 2021
    Posts:
    1
    Greetings all,

    I am trying to create a simple game where I am controlling the object ( let us say the object is a car or a ball) with flex sensor + arduino.
    I am a beginner in unity and for now I was able to use a push button to control the car movement its simpler since its either on or off, but when it comes to a sensor that has a range of values I am not sure how to write the code .

    I would appreciate if you can advise me on this.
    and if there are any tutorials on the subject that will be amazing.
     
  2. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,850
    Depends on what you try to do with the values range of the sensors. What is their range..what is the function they control? I assume you are assembling packets to send out the serial port t your arduino setup?
     
  3. sxa

    sxa

    Joined:
    Aug 8, 2014
    Posts:
    741
    It does depend a bit on how you want things to behave. But you might want to start by getting your bend sensor data to operate as a switch within your Unity code, so (raw arduino) values 0 to 511(*) count as 'off' while 512-1023(*) count as on.
    That way you can start by adapting the code that handles the Arduino input without having to change the code that handles movement.
    Once you have digital movement working via your analog sensor, then you can start thinking about how you change your movement code.(ie do you want more bending to mean faster movement...)

    (* ie what you get from analogRead(), assumption being 10-bit ADC input maximum on the Arduino's analog input)
     
    Voronoi likes this.
  4. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    584
    I agree with @sxa , get a very simple implementation going like making it a switch.

    I found with sensors the next issue you will run into is the stream of incoming data causes problems when running on the main thread. I ended up learning about opening a new thread to keep the game feeling fluid on screen. That is quite complicated, but I think important when dealing with raw sensor data, so start simple. There may be better solutions than opening a new thread, but that's what worked for me.