Search Unity

Tilt Functions Causes Lag in my Project

Discussion in 'Scripting' started by bekiryanik, Mar 23, 2015.

  1. bekiryanik

    bekiryanik

    Joined:
    Jul 6, 2014
    Posts:
    191
    Hello everyone,

    I use tilt codes below but when i tilt my character, game seems laggy and freezes sometimes. What could be the problem of this codes? Is this because of tiltBool or is this because of FixedUptade? Or, these codes are healty and there is something wrong with the other codes in my project. I am 99% sure that something wrong with these codes, because, whenever i use these codes, i get lag.

    Code (csharp):
    1.  
    2. void FixedUpdate ()
    3. {
    4. if(tiltBool == true)
    5. {
    6.  
    7. Vector3 accelerator = Input.acceleration;
    8.  
    9. euler.y += accelerator.x * turnSpeed;
    10. euler.z = Mathf.Lerp(euler.z, -accelerator.x * maxTurnLean, 0.2f);
    11. Quaternion rot = Quaternion.Euler(euler);
    12. transform.rotation = Quaternion.Lerp (transform.rotation, rot, sensitivity);
    13.  
    14.  
    15. Vector3 movement = new Vector3 (Input.acceleration.x, 0.0f, 0.0f);
    16. rigidbody.velocity = movement * fixLeft;
    17. }
    18.  
    19.  
    20. }
    21.  
    22.  
     
  2. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
  3. bekiryanik

    bekiryanik

    Joined:
    Jul 6, 2014
    Posts:
    191
    @MrPriest This answer is different then mine. I also tried to solve my lag problem with that solution either but it didn't work. Thanks for your answer!
     
  4. bekiryanik

    bekiryanik

    Joined:
    Jul 6, 2014
    Posts:
    191
    Any other idea what could be the problem with these codes?
     
  5. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    I'll try it today and see for myself how it works :)
    But I still think it's due to lerp being inside an update, after all, a lerp does a few actions, and an update is meant to do one action.
    So basically it's asking Unity to do a few actions inside one frame.
     
  6. bekiryanik

    bekiryanik

    Joined:
    Jul 6, 2014
    Posts:
    191
    Honestly, i understand now why you have doubts about lerp. :) I was using lerp under Update function because i had to update the current Z rotation of my player according to tilt rotation. So, where can use lerp if can't use it under Update function?

    Thank you!
     
  7. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Have you heard of coroutines?
    Basically, it's a function-like thing that executes one action, saves its state and progress compared to the target result, returns back to the caller (update function), and then on the next call (next update), does the next action, and so on.

    I've just went to look at the tutorial script, and it actually has lerp in it, lucky :)
    https://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines

    If it's not too much trouble, can you try it and tell me if it solves the lag?
    I'll try to do it myself if I can afford to (time-wise) today, in a few hours.
    But coroutines are very helpful, so I'd recommend giving it a try! (It's great for actions independent of the update timing, such as sending a unit to a location in a strategy game for instance!)
     
  8. Brominion

    Brominion

    Joined:
    Sep 30, 2012
    Posts:
    48
    Not sure where to start with this one.
    Update, coroutines, and fixed update all execute in sequence each frame, on the main thread, there is no magic-other-space-time in which co-routines operate independently.
    look here http://docs.unity3d.com/Manual/ExecutionOrder.html

    Lerp performs a "the same math" each time it is called. (interpolating between _from and _to , but some amount _t).
    it works because you normally use it to set the _from to the result.

    Depending on your device performance i would be suprised if a lerp or two was the problem. If by lag you mean there is a reduction in framerate, more likely it is caused by the camera turning so that large amount of game objects become visible, or something else.
     
  9. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    I did not mean to say that they are completely independent, just that if he uses update it will restart (or at least, not work as intended) each update.

    With coroutine you just set the from, to, time to execute once, and then call the coroutine every update, and it will do so without restarting the lerp from zero, or without setting a new from, to, time to execute.

    I had a sunstroke yesterday (In Israel it was "Good Deeds Day" and my company was outside with some kids with home issues) so I could not even look at a pc screen :<
    I'll have a look today!

    Also - I am no pro, my opinions are, well, just that, opinions. Sadly I do not have the time to play with Unity everyday, and if I do, I don't want to look at code that much (after 10+ hours of programming at work), so my understanding is limited, and not with too much experience.
    I think I should add that as a signature... I hate to mislead.

    Heh, I just did :)
     
  10. Brominion

    Brominion

    Joined:
    Sep 30, 2012
    Posts:
    48
    Did not mean to offend, but as you said, it is unfortunate if your advice is not accurate or teaches a way of thinking that will set him on the wrong track, or is based on a misunderstanding.
     
  11. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    None taken at all!
    I'd rather be told I am wrong, than let someone do something bad based on my advice.
    Also, I'll learn that way too :)
     
    Brominion likes this.
  12. bekiryanik

    bekiryanik

    Joined:
    Jul 6, 2014
    Posts:
    191
    I will use them in coroutines as soon as possible(When i have time to do that) and if it helps, i will let you know. Thanks for the advice. I will be waiting for the other advices about how to use Lerp much more correctly.
     
  13. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    I was given another code to look at, and I will do so today.
    If you'd like you can send me your project and I'll check it out myself in around 9 hours (after work).
    I'll look at it after the other project.
    I promise not to take anything from it, though I will not be offended if you choose not to (I'd refuse if I were you)
     
  14. bekiryanik

    bekiryanik

    Joined:
    Jul 6, 2014
    Posts:
    191
    :) As you thought so, i can't send my project. I would love to prepare a test project and send you that one but, i have no time to do that right now. Using the codes in coroutines will take a while because of having no time. If you do that before me, please let me know if it works or not.
     
  15. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Understandable, anyway I will give it a shot today.
    Can you at least tell me of the settings?
    Is it 1st Person, or 3rd Person camera?
    Is the scene filled with anything, or does it happen with an empty scene (As in, one without many objects around)?
     
  16. bekiryanik

    bekiryanik

    Joined:
    Jul 6, 2014
    Posts:
    191
    3rd person camera. You can create an infinite line on the screen to see the lag clearly when you use this script. Create a cylinder and attach this script to it. Then run the project on your android device and start tilting fastly. You will see the lag while tilting.
     
  17. bekiryanik

    bekiryanik

    Joined:
    Jul 6, 2014
    Posts:
    191
    Hello again,

    I am still working on this project and i was thinking that i fixed the lag problem with lerp but it seems that it wasn't fixed. I still have the same codes on my player but some of things are changed. Camera was 3rd person camera before but now, it is 1st person camera and while moving the player left and right with tilt, game screen moves by lagging. If i do not use tilt for moving my player, i do not see any lag. There is only one thing left, tilt codes are not healthy. Actually, instead of using this tilt codes in Coroutines, what can i use instead of "Lerp" function for changing Z rotation of my player?

    Thanks!
     
  18. bekiryanik

    bekiryanik

    Joined:
    Jul 6, 2014
    Posts:
    191
    I have just noticed that lag is not because of lerp function. it is because of the codes below;
    Code (csharp):
    1.  
    2. Vector3 movement = new Vector3 (Input.acceleration.x, 0.0f, 0.0f);
    3. rigidbody.velocity = movement * fixLeft;
    4.  
    The code you see above lets the player move left or right when tilt is detected. Now, i need to find something else to do the same job with this code.
     
  19. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    What a messy thread.

    Anyway, start over from the beginning. What are you actually observing? Lag refers to a delay between input and player response. Is this what you are seeing, or are you seeing a frame rate drop?

    These are two very different issues and require two different approaches. If its lag I would simply scale up the value of the input. If its frame rate I would get out the profiler.
     
  20. bekiryanik

    bekiryanik

    Joined:
    Jul 6, 2014
    Posts:
    191
    Actually there are no two different issues now. Tilt function which i gave in the first post of the thread for the rotation of the object, works very well. The problem is with the move function depending on tilting device. I am trying to move X position of my object by tilting my device. Actually, there are lots of way to do that but some of them acts weird. The player has no connection with any mesh renderer or box collider on scene. It moves depending on call from the codes. For example;
    Code (csharp):
    1.  
    2. transform.localPosition = newVector3(Input.acceleration.x, 0, 0);
    3.  
    I tried to use this code for moving the object but when i tilt my device, player starts to go down.

    For short, i would like to write a script which will help to move my player on the X axis smootly.
     
  21. bekiryanik

    bekiryanik

    Joined:
    Jul 6, 2014
    Posts:
    191
    I have fixed the lag problem with this tilt moving codes you see below, i wanted to share this in here for people who searches something like this;
    Code (csharp):
    1.  
    2. public float speed = 5;
    3.  
    4. void Update () {
    5.  
    6. Vector3 posToGo = new Vector3((Input.acceleration.x*speed)+(transform.localPosition.x), transform.localPosition.y, transform.localPosition.z);
    7. transform.localPosition = Vector3.Lerp(transform.localPosition, posToGo, Time.deltaTime);
    8. }
    9.