Search Unity

Jerky problem in 3D game.

Discussion in 'Scripting' started by Simonxzx, Jun 17, 2015.

  1. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    Hi everyone, I'm developing a 3D Maze with Unity. In this Maze there's a main character (actually a cube) that moves with a certain speed within the maze. Everything works fine when I debug with the Unity player, but when I build the game to my Android phone the cube goes jerky as it moves, and so the surrounding environment. The only script I have is that one that controls user's touch to make the cube turn right if the user swipes right, to make the cube turn left if user swipes left etc..
    Do you have any idea ? Thank you very much.
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    PEBKAC.

    If you want help, post code. Not a description of the problem.
     
  3. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    Yeah just show us the movement part of your script. So that jerky movement, hmm. Could be that your movement is in Fixed Update, or maybe your using some recast for movement.
     
  4. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    Hi guys, and thank you for your replies, here's my script.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class WalkAndSwipe : MonoBehaviour
    5. {
    6.     public float minSwipeDistY = 90f;
    7.     public float minSwipeDistX = 90f;
    8.     public float Force = 20f;
    9.     private Vector2 startPos;
    10.     public GameObject Protagonist;
    11.  
    12.     void Update()
    13.     {
    14.         Protagonist.GetComponent<Rigidbody>().AddForce(transform.forward * Force);
    15.  
    16. //#if UNITY_ANDROID
    17.         if (Input.touchCount > 0)
    18.            
    19.         {
    20.             Touch touch = Input.touches[0];
    21.    
    22.             switch (touch.phase)
    23.                
    24.             {
    25.                
    26.             case TouchPhase.Began:
    27.  
    28.                 startPos = touch.position;
    29.                 Debug.Log("Began");
    30.                 Force = 0.0f;
    31.                 Protagonist.GetComponent<Rigidbody>().AddForce(transform.forward * -Force);
    32.                 break;
    33.  
    34.                 float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
    35.  
    36.                     if (swipeDistVertical > minSwipeDistY)
    37.                     {
    38.  
    39.                         float swipeValue = Mathf.Sign(touch.position.y - startPos.y);
    40.  
    41.                         if (swipeValue > 0)
    42.                         {//up swipe
    43.                             Force = 500f;
    44.                             Debug.Log("Up");
    45.                         }
    46.  
    47.                         else if (swipeValue < 0)
    48.                         {//down swipe
    49.                             Debug.Log("Down");
    50.                         }
    51.  
    52.  
    53.                     }
    54.  
    55.                     float swipeDistHorizontal = (new Vector3(touch.position.x, 0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
    56.  
    57.                     if (swipeDistHorizontal > minSwipeDistX)
    58.                     {
    59.  
    60.                         float swipeValue = Mathf.Sign(touch.position.x - startPos.x);
    61.  
    62.                         if (swipeValue > 0)
    63.                         {//right swipe
    64.                             Protagonist.transform.Rotate(0, 90, 0);
    65.                             Force = 500f;
    66.                             Debug.Log("Right");
    67.                         }
    68.  
    69.                         else if (swipeValue < 0)
    70.                         { //left swipe
    71.                             Protagonist.transform.Rotate(0, -90, 0);
    72.                             Force = 500f;
    73.                             Debug.Log("Left");
    74.                         }
    75.  
    76.                     }
    77.                 break;
    78.             }
    79.         }
    80.     }
    81. }
     
  5. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    My guess is that your game is lowering in framerate when on the device, and a lowering framerate causes your jerkiness.
     
  6. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    So how can I solve that ?
     
  7. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Make all movement framerate independent. If you're adding force, add that force * Time.deltaTime. If you're moving an object by hand, move it * Time.deltaTime.
     
  8. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    Okay, I tried to do this, but nothing changed :(

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class WalkAndSwipe : MonoBehaviour
    5. {
    6.     public float minSwipeDistY = 90f;
    7.     public float minSwipeDistX = 90f;
    8.     public float Force = 10250;
    9.     private Vector2 startPos;
    10.     public GameObject Protagonist;
    11.  
    12.     void Update()
    13.     {
    14.         Protagonist.GetComponent<Rigidbody>().AddForce(transform.forward * Time.deltaTime * Force);
    15.  
    16.         //#if UNITY_ANDROID
    17.         if (Input.touchCount > 0)
    18.         {
    19.             Touch touch = Input.touches[0];
    20.             switch (touch.phase)
    21.             {
    22.                 case TouchPhase.Began:
    23.                     startPos = touch.position;
    24.                     Debug.Log("Began");
    25.                     Force = 0.0f;
    26.                     Protagonist.GetComponent<Rigidbody>().AddForce(transform.forward * -Time.deltaTime * Force);
    27.                     break;
    28.  
    29.                 case TouchPhase.Ended:
    30.  
    31.                     float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
    32.  
    33.                     if (swipeDistVertical > minSwipeDistY)
    34.                     {
    35.                         float swipeValue = Mathf.Sign(touch.position.y - startPos.y);
    36.  
    37.                         if (swipeValue > 0) //up swipe
    38.                         {
    39.                             Force = 10250f;
    40.                             Debug.Log("Up");
    41.                         }
    42.                         else if (swipeValue < 0) //down swipe
    43.                         {
    44.                             Debug.Log("Down");
    45.                         }
    46.                     }
    47.  
    48.                     float swipeDistHorizontal = (new Vector3(touch.position.x, 0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
    49.  
    50.                     if (swipeDistHorizontal > minSwipeDistX)
    51.                     {
    52.                         float swipeValue = Mathf.Sign(touch.position.x - startPos.x);
    53.  
    54.                         if (swipeValue > 0) //right swipe
    55.                         {
    56.                             Protagonist.transform.Rotate(0, 90, 0);
    57.                             Force = 10250f;
    58.                             Debug.Log("Right");
    59.                         }
    60.                         else if (swipeValue < 0) //left swipe
    61.                         {
    62.                             Protagonist.transform.Rotate(0, -90, 0);
    63.                             Force = 10250f;
    64.                             Debug.Log("Left");
    65.                         }
    66.                     }
    67.  
    68.                     break;
    69.             }
    70.         }
    71.     }
    72. }
     
  9. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Well, if that didn't fix it, then your framerate is fluctuating for some reason. Maybe there are too many pieces of art loading and unloading for it to keep the rate high and constant? How many unique meshes and objects do you have in the scene?
     
  10. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    I've my 3D maze and the things you see in the pic I attached here
     

    Attached Files:

  11. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Ah. Yes. Mobile devices tend to chug and die horribly when looking at Unity terrain objects, and the jerkiness is different parts of the terrain being more or less chuggy.

    To make it smooth, eliminate the terrain from your scene entirely.
     
  12. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    What's the meaning of "remove the terrain entirely" ?
    However, thank you for spending your time with my problem ! :)
     
  13. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    There's a Unity editor object called a "Terrain" in your scene. Select it in the hierarchy window and delete it.
     
  14. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    Okay, but if I do so my maze disappears.
     
  15. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Then you need to make your maze out of something else besides the terrain object. It simply will not run on phones effectively.

    I would recommend Unity built-in cube objects, personally. Just shape them and position them as needed.
     
  16. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    Well, thank you very much. I think I'll do so. :)
     
  17. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Have you tried running the profiler?

    Watch out for memory allocations especially. I imagine they will cripple most devices, since they have an effect on desktop
     
  18. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    @JamesLeeNZ , I think 'autoconnect profiler' function is only avaiable for Pro users :(