Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Unity coordinates and positioning from JSON file

Discussion in 'Scripting' started by Deivydas4, Jun 19, 2023.

  1. Deivydas4

    Deivydas4

    Joined:
    Oct 28, 2021
    Posts:
    16
    Hey! I have a task where I need to read JSON file, which consists of level_data (the coordinates where dots should be).

    Code (JavaScript):
    1. {
    2.     "levels":[
    3.     { "level_data": [ "50", "50", "950", "50", "950", "950", "50", "950" ] },
    4. }

    I wrote a code which takes these coordinates, but when I try to test my game, they are appearing outside my game view. The quote from the task "X and Y positions can vary from 0 to 1000. Coordinate space is centered at top left corner, so Y position is inverted (Y increases when going down). Make sure to convert positions to Unity coordinate space correctly"

    This is my code:

    Code (CSharp):
    1.     void CreatePointsForLevel(Level level)
    2.     {
    3.         for (int i = 0; i < level.level_data.Count; i += 2)
    4.         {
    5.             float x = float.Parse(level.level_data[i]);
    6.             float y = float.Parse(level.level_data[i + 1]);
    7.  
    8.             // Convert from your coordinate system to Unity's
    9.             Vector2 position = new Vector2(x, Screen.height - y);
    10.  
    11.             // Instantiate a new point
    12.             GameObject newPoint = Instantiate(pointPrefab, position, Quaternion.identity);
    13.  
    14.             // Assign the sequence number and increase it for the next point
    15.             PointBehavior pointBehaviour = newPoint.GetComponent<PointBehavior>();
    16.             pointBehaviour.sequenceNumber = currentSequenceNumber++;
    17.         }
    18.  
    19.         currentSequenceNumber = 1;
    20.     }
    The question I ask is how to make them to appear in game view and won't be out of bounds of camera?

    Thank you.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Reading JSON is not related to 2D features.

    I'l remove the tag and move your post to the scripting forum for you.
     
    venediklee likes this.
  3. venediklee

    venediklee

    Joined:
    Jul 24, 2017
    Posts:
    143
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    This sounds like schoolwork to me. Break it down like any problem.

    Ask yourself a million times "Can I ....?" Just like this guy:

    Imphenzia: How Did I Learn To Make Games:



    In general I highly suggest staying away from Unity's JSON "tiny lite" package. It's really not very capable at all and will silently fail on very common data structures, such as bare arrays, tuples, Dictionaries and Hashes and ALL properties.

    Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window -> Package Manager).

    https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347

    Also, always be sure to leverage sites like:

    https://jsonlint.com
    https://json2csharp.com
    https://csharp2json.io