Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by chadmanila · Apr 03, 2017 at 06:49 AM · lagarduinoserialport

very long LAG in my unity game using arduino

im a first time user of unity3d and ive seen a tutorial on a car simulation using arduino and unity . i followed every steps in the tutorial. i even copy and paste the code for the unity . and it's already running but the problem is it has a 30 seconds delay before reading the inputs that my arduino is sending. here are the codes that im using and here is the tutorial that ive been follwing. hope someone could help me. i really dont know what to do. https://www.youtube.com/watch?v=N9RhSa8oMD8&t=414s

arduino code

 int carRotation=0;          //variable to read the values from the potentiometers
 int carSpeed=0;
 
 void setup() {
   Serial.begin(9600);       //start the serial communication
   pinMode(A0,INPUT);        //initialize the potentiometers inputs
   pinMode(A1,INPUT);
 }
 
 void loop() {
   carRotation = analogRead(A0); //read the values of the potentiometers - between 0 and 1023
   carSpeed = analogRead(A1);
   Serial.print(map(carSpeed,0,1023,-100,100)); //scale it to use it with the car speed
   Serial.print(",");
   Serial.println(map(carRotation,0,1023,-100,100)); //scale it for the car rotation
   delay(50);
 }

car user control code

 using System;
 using UnityEngine;
 using UnityStandardAssets.CrossPlatformInput;
 using System.IO.Ports; //Library to read our ardunio data
 using System.Collections;//to use Ienumarator in our code
 
 namespace UnityStandardAssets.Vehicles.Car
 {
     [RequireComponent(typeof (CarController))]
     public class CarUserControl : MonoBehaviour
     {
         private CarController m_Car; // the car controller we want to use
 
   SerialPort seri = new SerialPort("COM6",9600);//define our port
         private void Awake()
         {
             // get the car controller
    seri.Open();//open our port
    StartCoroutine(ReadDataFromSerialPort());//start loop
             m_Car = GetComponent<CarController>();
          
   }
 
   IEnumerator ReadDataFromSerialPort(){
    while(true){//loop
    string[] values = seri.ReadLine ().Split (',');//we split our string value by , because we write string as carspeed,cartotation in our ardunio codes
    v = (float.Parse(values[0])) / 100;
    h = (float.Parse (values [1])) / 100;
    yield return new WaitForSeconds (.05f);//waiting seconds to read data. It should be same as ardunio code loop delay
    }
   }
 
 
 
   float h;//horizontal (car direction)
   float v;//vertical (car speed)
         private void FixedUpdate()
         {
             // pass the input to the car!
    //Close old codes which created by unity team
             //float h = CrossPlatformInputManager.GetAxis("Horizontal");
             //float v = CrossPlatformInputManager.GetAxis("Vertical");
 #if !MOBILE_INPUT
             float handbrake = CrossPlatformInputManager.GetAxis("Jump");
             m_Car.Move(h, v, v, handbrake);
 #else
             m_Car.Move(h, v, v, 0f);
 #endif
         }
     }
 }

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 piratmak · Oct 02, 2017 at 10:14 AM

I have the same problem and I used the same tutorial, anny solution found can help me???

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
avatar image
0

Answer by CometsVR · Aug 17, 2020 at 05:46 PM

You have delay(50) in your code.

Remove and check it out.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Arduino Unity3d FPS drop after twisting Potentiometer 0 Answers

unity to serial ports ( blink LED ) 0 Answers

Arduino serial delegate help 1 Answer

transform.rotation = ... reacts slow 2 Answers

Need Help Connecting Arduino to Unity 0 Answers


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