Search Unity

Whats is the maximum value of the x y and z cordinates..?

Discussion in '2D' started by hkk007, Feb 18, 2015.

  1. hkk007

    hkk007

    Joined:
    Feb 2, 2015
    Posts:
    5
    I am new to unity and I am designing a infinite running 2d game.
    There are 2 ways to create backgrounds
    1. scrolling the background not changing the position
    2. spawn the background and moving the player forward.

    In the second approach x coordinate increases.
    Which is best approach ?
    What is the maximum limit of the x coordinate ?


    Thanks in advance
     
  2. justJack

    justJack

    Joined:
    Feb 5, 2015
    Posts:
    22
    Well,
    you can creat a Quad, scale it to fit your camera properly, rotate if necessary, and attach a material (a texture + shader).
    In order to create the illusion of a scrolling background, you have to offset the texture.

    Here: http://docs.unity3d.com/ScriptReference/Material.SetTextureOffset.html
    And here: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/2d-scrolling-backgrounds
    Also here: http://answers.unity3d.com/questions/419430/is-recomended-to-use-settextureoffset-to-create-a.html

    Good luck.
     
  3. hkk007

    hkk007

    Joined:
    Feb 2, 2015
    Posts:
    5
    Sir,
    Thanks for the reply.
    Will u please tell me which is the best procedure ? scrolling the background by keeping player x axis constant or moving the player forward by incrementing the x axis?
     
  4. justJack

    justJack

    Joined:
    Feb 5, 2015
    Posts:
    22
    Well,
    it is not a good idea to work with a big number. The calculation will be expensive and will take longer , which is a pain for any mobile device.
    With this in mind, the optimal solution would be to just offset the texture as moving the player will increase the X position too much eventually.

    Then, to create the illusion of running endlessly, just keep the character at the same position all the time, create a running animation, scroll the background and... You are done :)

    It is not hard.
     
    Last edited: Feb 18, 2015
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The size of the number is completely irrelevant; 2 * 2 is exactly as expensive as 1234567 * 1234567.

    The problem with floating point is that it loses precision as the number gets larger, so in most cases you'd want to keep coordinates within 10K units of the origin.

    More like 3 ways:

    3. After the character has moved a certain distance, move everything back by exactly that distance, repeat as necessary.

    Setting the material offset has issues because mobile devices tend to have very limited UV precision, so as the UVs get farther away from the 0..1 range, the texture starts distorting.

    --Eric
     
  6. justJack

    justJack

    Joined:
    Feb 5, 2015
    Posts:
    22
    Yep, I know. I expressed myself poorly. What I mean is that 12.3454321* 54.3212345 might be more expensive than 123 * 123.

    Actually, I remember when I was looking for an optimal way to scroll my background too. Some suggested to use Time.time and Mathf.Repeat, but some were complaning about the performance on mobile due to Time.time being rapidly increased, making the operation somehow costly for a mobile device because a float is a very precise value.

    I do not know if it is right by the way, but since then I am avoiding calculations with stupidly high and precise values.
     
    Last edited: Feb 18, 2015
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Only if you're actually using different types, but not necessarily even then, since it depends on the CPU.

    That's completely wrong. The CPU doesn't care what the number is, and it takes the same number of clock cycles to complete regardless. Using Time.time and Mathf.Repeat is an utterly trivial operation on even the oldest mobile device.

    Stop avoiding that. ;) I'd strongly recommend some computer science courses, so you can sort out the bad advice.

    --Eric
     
    theANMATOR2b likes this.