Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by cgressel · Oct 11, 2018 at 04:04 PM · transformarduinoserialportserial

Transform Unity Object with Arduino

hey guys hope you can help me! i try to use the serial numbers from my arduino board to transform the object in unity. on arduino site my code is really simple and looks like this:

   void setup() {
   // put your setup code here, to run once:
   Serial.begin(9600);
 }
 
 void loop() {
   // put your main code here, to run repeatedly:
   int a = analogRead(A0);
   //Sleep for 50ms, which provides the recommended sample rate (20Hz)
   delay(500);
   Serial.println(a);
 }

in the serial monitor from arduino i get these values (picture)alt text

now i want to use these values to constantly transform my geometry (a cube) in unity. for example if the value from arduino is 50 so the cube should be scaled by 50

do you know how this can work???

thanks in advance

unbenannt.jpg (27.9 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Glurth · Oct 11, 2018 at 04:32 PM

You need to put that code in a custom MonoBehavior class. Then, you will be able to make that loop function, your monobehavior's "Update" function instead (and setup() stuff inside the monobehavior's Start() function). I would suggest you create a cube in your scene using the unity menu, then ADD this new monobehavior component you just created, to the gameobject.

Monobehavior classes, when attached to a game object as a component, can access the object's Transform. This is how you will change it's size, inside Update():

 transform.localScale= Vector3.one * analogRead(A0);


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cgressel · Oct 11, 2018 at 05:44 PM 1
Share

hey thanks for your answer....so it doesnt sound that easy any more :D

do you think (on unity side) it could work with something like this?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.IO.Ports;
 
 public class Scale : $$anonymous$$onoBehaviour
 {
 
     private float analogRead;
 
     SerialPort sp = new SerialPort("CO$$anonymous$$3", 9600);
 
 
     // Use this for initialization
     void Start()
     {
 
         sp.Open();
         sp.ReadTimeout = 1;
 
 
     }
 
     // Update is called once per frame
     void Update()
     {
 
         analogRead = analogRead;
 
         if (sp.IsOpen)
         {
             ScaleObject(sp.ReadByte());
         }
     }
 
     void ScaleObject(int analogRead)
     {
         transform.localScale = Vector3.one * analogRead;
 
     }
 }

kind regards

avatar image Glurth cgressel · Oct 11, 2018 at 07:33 PM 0
Share

yes, that looks good to me, how does it work?

$$anonymous$$ay I suggest you make the analogRead variable, public ins$$anonymous$$d of private? That way, you will be able to see its value change, in the unity editor inspector.

$$anonymous$$eep in $$anonymous$$d: the Update function only runs when you hit "play" in the editor (or run the final build). If you want it to execute BEFORE you hit play, while in the editor, you will need to apply the ExecuteInEdit$$anonymous$$ode attribute to your monobehavior class.

avatar image
0

Answer by cgressel · Oct 11, 2018 at 09:38 PM

yeah tried to change it to public but you can not see a change, i mean it gets closer but still not the thing that i want

now i have the problem that the geometry gets some kind of "disco flash light"-effect because the scale always goes back to the original value...for example 34,1,23,1,33,1,12,1 and so on

never thought that this could be so tricky

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

119 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sending Data From Unity To Arduino 0 Answers

Two Arduino Boards for Same project 0 Answers

Arduino with Unity: Bad Framerate! 3 Answers

send string to serial port on android 0 Answers

Problems With Sending Data through Serial Port 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges