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.
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.
Hi guys, and thank you for your replies, here's my script. Code (CSharp): using UnityEngine; using System.Collections; public class WalkAndSwipe : MonoBehaviour { public float minSwipeDistY = 90f; public float minSwipeDistX = 90f; public float Force = 20f; private Vector2 startPos; public GameObject Protagonist; void Update() { Protagonist.GetComponent<Rigidbody>().AddForce(transform.forward * Force); //#if UNITY_ANDROID if (Input.touchCount > 0) { Touch touch = Input.touches[0]; switch (touch.phase) { case TouchPhase.Began: startPos = touch.position; Debug.Log("Began"); Force = 0.0f; Protagonist.GetComponent<Rigidbody>().AddForce(transform.forward * -Force); break; float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude; if (swipeDistVertical > minSwipeDistY) { float swipeValue = Mathf.Sign(touch.position.y - startPos.y); if (swipeValue > 0) {//up swipe Force = 500f; Debug.Log("Up"); } else if (swipeValue < 0) {//down swipe Debug.Log("Down"); } } float swipeDistHorizontal = (new Vector3(touch.position.x, 0, 0) - new Vector3(startPos.x, 0, 0)).magnitude; if (swipeDistHorizontal > minSwipeDistX) { float swipeValue = Mathf.Sign(touch.position.x - startPos.x); if (swipeValue > 0) {//right swipe Protagonist.transform.Rotate(0, 90, 0); Force = 500f; Debug.Log("Right"); } else if (swipeValue < 0) { //left swipe Protagonist.transform.Rotate(0, -90, 0); Force = 500f; Debug.Log("Left"); } } break; } } } }
My guess is that your game is lowering in framerate when on the device, and a lowering framerate causes your jerkiness.
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.
Okay, I tried to do this, but nothing changed Code (CSharp): using UnityEngine; using System.Collections; public class WalkAndSwipe : MonoBehaviour { public float minSwipeDistY = 90f; public float minSwipeDistX = 90f; public float Force = 10250; private Vector2 startPos; public GameObject Protagonist; void Update() { Protagonist.GetComponent<Rigidbody>().AddForce(transform.forward * Time.deltaTime * Force); //#if UNITY_ANDROID if (Input.touchCount > 0) { Touch touch = Input.touches[0]; switch (touch.phase) { case TouchPhase.Began: startPos = touch.position; Debug.Log("Began"); Force = 0.0f; Protagonist.GetComponent<Rigidbody>().AddForce(transform.forward * -Time.deltaTime * Force); break; case TouchPhase.Ended: float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude; if (swipeDistVertical > minSwipeDistY) { float swipeValue = Mathf.Sign(touch.position.y - startPos.y); if (swipeValue > 0) //up swipe { Force = 10250f; Debug.Log("Up"); } else if (swipeValue < 0) //down swipe { Debug.Log("Down"); } } float swipeDistHorizontal = (new Vector3(touch.position.x, 0, 0) - new Vector3(startPos.x, 0, 0)).magnitude; if (swipeDistHorizontal > minSwipeDistX) { float swipeValue = Mathf.Sign(touch.position.x - startPos.x); if (swipeValue > 0) //right swipe { Protagonist.transform.Rotate(0, 90, 0); Force = 10250f; Debug.Log("Right"); } else if (swipeValue < 0) //left swipe { Protagonist.transform.Rotate(0, -90, 0); Force = 10250f; Debug.Log("Left"); } } break; } } } }
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?
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.
What's the meaning of "remove the terrain entirely" ? However, thank you for spending your time with my problem !
There's a Unity editor object called a "Terrain" in your scene. Select it in the hierarchy window and delete it.
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.
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