Search Unity

My game looks fine in the unity editor but when in Android it doesnt work

Discussion in 'Scripting' started by acimutstudios2, Sep 23, 2018.

  1. acimutstudios2

    acimutstudios2

    Joined:
    Apr 30, 2018
    Posts:
    11
    I created a simple game for android and everything looks fine in the pc, I have some pictures how it should look in the phone (and how it looks in the unity editor) and the last one is how it looks on the phone, the player doesnt even appear and it looks like its going down for ever (also added my project Hierarchy). Pc.PNG Hierarchy.PNG 4e9dfd04-19c7-4201-b5fb-0186b734670b.JPG
     
  2. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    This looks like a hierarchical problem. How is the background rendered? It's probably on top of the player object.
     
  3. acimutstudios2

    acimutstudios2

    Joined:
    Apr 30, 2018
    Posts:
    11
    But why does it work on my pc but not on my Android phone?.
    I do it inside a class since its a endless runner, but this function spawns the background picture:

    Code (CSharp):
    1. private GameObject ImageCreation()
    2.     {
    3.         GameObject go = Instantiate(imageBackground) as GameObject;
    4.         go.transform.SetParent(transform, false);
    5.         go.transform.localPosition = new Vector3(go.transform.localPosition.x - width + (positionx),height,depth);
    6.         positionx += width;
    7.         return go;
    8.     }
     
  4. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    Alright, first test to see if this is the problem: comment-out all calls to this function so there is no background, and run it on Android. If this is the problem, then you'll be able to see your player.

    To fix the problem, you need to set the background to be the first child of wherever you're instantiating it.
    So put this after setting the parent of the instantiated background:
    Code (CSharp):
    1. go.transform.SetAsFirstSibling();
    I have no idea why it works on PC but not Android unfortunately.
     
  5. acimutstudios2

    acimutstudios2

    Joined:
    Apr 30, 2018
    Posts:
    11
    I removed the background but I cant see either the player nor the enemies, maybe it has something to do with my cameras?, I have 3 cameras, one to render the UI, other to follow the player and the last one to render the background
     
  6. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    Hmmm, yeah that could also be a problem. Have you set the 'depth' property of each camera? Maybe set your player camera to have a higher depth value than the background, and have the UI camera depth above all.
     
  7. acimutstudios2

    acimutstudios2

    Joined:
    Apr 30, 2018
    Posts:
    11
    Yeah all my cameras are set to 'depth only', the player camera its set to -99, the background one to 10 and the UI one to 0. But maybe I could play a bit with the depths and see? Im also making the player camera a child of the canvas at runtime.
     
  8. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    Try setting the player camera depth to like 5 or something, and leave the others as they are, if that doesn't work then I have no idea what else could be causing your problem.
     
  9. acimutstudios2

    acimutstudios2

    Joined:
    Apr 30, 2018
    Posts:
    11
    Ok I saw my problem, the camera was rendering in a wrong place, I fixed it by attaching it to the player, but do you know how can I make a good off set for my camera movement so I dont need to attach it to the player?

    EDIT: btw thanks for all the help :D
     
  10. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    You mean like give the camera the same position as the player? You could just set the position to be the same if that's what you mean.

    You're welcome btw ;)
     
    Last edited: Sep 23, 2018
  11. acimutstudios2

    acimutstudios2

    Joined:
    Apr 30, 2018
    Posts:
    11
    I mean I made a script for a smooth camera follow, it works amazing in my pc but not on my phone apparently, I believe its because im hard coding the offset, but I dont know how could I set the metrics dynamically (like getting the screen size or something). This is my script:
    Code (CSharp):
    1. private Vector3 SetUpOffset()
    2.     {
    3.         float x = 700f;
    4.         float y = canvas.rect.height/7;
    5.         float z = -99f;
    6.  
    7.         return new Vector3(x, y, z);
    8.     }
    And this is to use this position and make the smooth movement:
    Code (CSharp):
    1.  private void CreateSmoothMovement()
    2.     {
    3.         if(playerTransform != null)
    4.         {
    5.             Vector3 desiredPosition = playerTransform.localPosition + offset;
    6.             Vector3 smoothPosition = Vector3.Lerp(transform.localPosition, desiredPosition, speed * Time.fixedDeltaTime);
    7.  
    8.             transform.localPosition = smoothPosition;
    9.         }  
    10.     }
     
  12. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    Okay, yeah maybe try adjust your offsets to be based off the screen width and height.
    https://docs.unity3d.com/ScriptReference/Screen-width.html
    https://docs.unity3d.com/ScriptReference/Screen-height.html
    Also, why did you set the z-offset to -99.0f? I believe 0 should be fine.

    Also, this isn't related to your current problem: In your CreateSmoothMovement function, I noticed you're using Time.fixedDeltaTime instead of Time.deltaTime. You probably don't want to do this. So change it to Time.deltaTime, and if you're calling that function from FixedUpdate, you should change it to LateUpdate. Camera follow scripts should be done in LateUpdate, not FixedUpdate, as FixedUpdate should only be used for physics-related things.
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.LateUpdate.html
     
  13. acimutstudios2

    acimutstudios2

    Joined:
    Apr 30, 2018
    Posts:
    11
    Thanks a lot for the links gonna get to work on it. -99 was just a random big number to test something haha but its back to 0.
    About the time.deltaTime, why its better to use time.deltaTime? I usually use time.fixedDeltaTime for anything related with movement didnt know it was bad :eek: . And yeah you are right, im calling it from fixedUpdate gonna change it to lateUpdate ASAP, thanks a lot.
     
  14. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    By default, Time.fixedDeltaTime is always like 0.02 or something. (And doesn't change unless you change it.) It should only be used for physics, as physics need to be called over a fixed time interval in order to be stable.

    When Time.deltaTime is called in any function except FixedUpdate (So like LateUpdate, Update, etc) it is always changing. It is the time (in seconds) since the last frame. For example if your game was running at 60fps, Time.deltaTime would be 1.0/60.0= 0.0166.... seconds.

    Also, when you try get Time.deltaTime in FixedUpdate, it returns Time.fixedDeltaTime.

    Sorry if my explanation was unclear, if you still don't understand, just search around on Google :)
     
  15. acimutstudios2

    acimutstudios2

    Joined:
    Apr 30, 2018
    Posts:
    11
    Oh no It makes a lot of sense, I guess thats why it has fixed in it haha, gonna try to fix my game now, thanks a lot for everything.
     
  16. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    Hahaha, you're welcome. Good luck fixing your game ;)