Search Unity

Creating updating gameobjects dependent on the surrounding gameobjects

Discussion in 'Scripting' started by MaxVuksan, Sep 16, 2019.

  1. MaxVuksan

    MaxVuksan

    Joined:
    Apr 21, 2019
    Posts:
    5
    I am attempting create gameobjects that change sprite depending on their surrounding sprites (similar to terraria). I have used Physics2D.OverlapCircle to detect what blocks around and adjust the sprites that one. This did work but because this was running on every single gameobject the project was extremly slow. Any suggestions or help would be amazing, thank you <3 upload_2019-9-16_19-2-58.png upload_2019-9-16_19-2-58.png

     
  2. Neriad

    Neriad

    Joined:
    Feb 12, 2016
    Posts:
    125
    You only need one overlap circle, so this should not be slow at all.
    One circle that tells every block to change sprite.
     
  3. MaxVuksan

    MaxVuksan

    Joined:
    Apr 21, 2019
    Posts:
    5
    I think you're confused on what I'm trying to achieve.
    I'm saying that when for example I place a block next to another the sprites change to match fluently
     

    Attached Files:

  4. MaxVuksan

    MaxVuksan

    Joined:
    Apr 21, 2019
    Posts:
    5
    Im not sure if this makes sense but what i've tried to do is find all game objects in an array and for each of those adjust their sprites depending on their surrounding blocks. This script is definitely not complete but i'm not sure if i'm going in the right direction..?

    All the variables are to represent the different sprites I have created to suit any surrounding block combination. The use of an array is to include sprites for a variety of blocks

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. public class BlockOptimisation : MonoBehaviour
    7. {
    8.     public Sprite[] LTCorner;// 1 = grass, 2 = dirt
    9.     public Sprite[] TFlat;// 1 = grass, 2 = dirt
    10.     public Sprite[] RTCorner;// 1 = grass, 2 = dirt
    11.     public Sprite[] BRMicroCorner;// 1 = grass, 2 = dirt
    12.     public Sprite[] BLMicroCorner;// 1 = grass, 2 = dirt
    13.     public Sprite[] LWall;// 1 = grass, 2 = dirt
    14.     public Sprite[] Full;// 1 = grass, 2 = dirt
    15.     public Sprite[] RWall;// 1 = grass, 2 = dirt
    16.     public Sprite[] TRMicroCorner;// 1 = grass, 2 = dirt
    17.     public Sprite[] TLMicroCorner;// 1 = grass, 2 = dirt
    18.     public Sprite[] LBCorner;// 1 = grass, 2 = dirt
    19.     public Sprite[] BFlat;// 1 = grass, 2 = dirt
    20.     public Sprite[] RBCorner;// 1 = grass, 2 = dirt
    21.     public Sprite[] TopDoubleMicroCorner;// 1 = grass, 2 = dirt
    22.     public Sprite[] BottomDoubleMicroCorner;// 1 = grass, 2 = dirt
    23.     public Sprite[] LeftDoubleMicroCorner;// 1 = grass, 2 = dirt
    24.     public Sprite[] RightDoubleMicroCorner;// 1 = grass, 2 = dirt
    25.     public Sprite[] ExTLTripleMicroCorner; // 1 = grass, 2 = dirt
    26.     public Sprite[] ExTRTripleMicroCorner;// 1 = grass, 2 = dirt
    27.     public Sprite[] ExBRTripleMicroCorner;// 1 = grass, 2 = dirt
    28.     public Sprite[] ExBLTripleMicroCorner;// 1 = grass, 2 = dirt
    29.     public Sprite[] QuadMicroCorner;// 1 = grass, 2 = dirt
    30.     public Sprite[] LRWall;// 1 = grass, 2 = dirt
    31.     public Sprite[] TBWall;// 1 = grass, 2 = dirt
    32.     public Sprite[] ExRWall;// 1 = grass, 2 = dirt
    33.     public Sprite[] ExBWall;// 1 = grass, 2 = dirt
    34.     public Sprite[] ExLWall;// 1 = grass, 2 = dirt
    35.     public Sprite[] ExTWall;// 1 = grass, 2 = dirt
    36.     public Sprite[] LeftDoubleMicroCorner_WithOppoWall; // 1 = grass, 2 = dirt
    37.     public Sprite[] TopDoubleMicroCorner_WithOppoWall;// 1 = grass, 2 = dirt
    38.     public Sprite[] RightDoubleMicroCorner_WithOppoWall;// 1 = grass, 2 = dirt
    39.     public Sprite[] BottomDoubleMicroCorner_WithOppoWall;// 1 = grass, 2 = dirt
    40.     public Sprite[] TLCornerWithMicroCorner;// 1 = grass, 2 = dirt
    41.     public Sprite[] TRCornerWithMicroCorner;// 1 = grass, 2 = dirt
    42.     public Sprite[] BRCornerWithMicroCorner;// 1 = grass, 2 = dirt
    43.     public Sprite[] BLCornerWithMicroCorner;// 1 = grass, 2 = dirt
    44.     public Sprite[] QuadWalls; // 1 = grass, 2 = dirt
    45.  
    46.     public LayerMask BlockLayer;
    47.     private GameObject[] DirtBlocks;
    48.     private SpriteRenderer sc; //sc stands for sprite renderer
    49.  
    50.     private void Update()
    51.     {
    52.      
    53.         DirtBlocks = GameObject.FindGameObjectsWithTag("DirtBlocks");
    54.  
    55.         foreach(GameObject blocks in DirtBlocks)
    56.         {
    57.             bool Ab_Collided = Physics2D.OverlapCircle(blocks.transform.position + new Vector3(0, 0.5f), 0.2f, BlockLayer);
    58.             bool AbL_Collided = Physics2D.OverlapCircle(blocks.transform.position + new Vector3(-0.5f, 0.5f), 0.2f, BlockLayer);
    59.             bool AbR_Collided = Physics2D.OverlapCircle(blocks.transform.position + new Vector3(0.5f, 0.5f), 0.2f, BlockLayer);
    60.             bool L_Collided = Physics2D.OverlapCircle(blocks.transform.position + new Vector3(-0.5f, 0f), 0.2f, BlockLayer);
    61.             bool R_Collided = Physics2D.OverlapCircle(blocks.transform.position + new Vector3(0.5f, 0), 0.2f, BlockLayer);
    62.             bool Be_Collided = Physics2D.OverlapCircle(blocks.transform.position + new Vector3(0, -0.5f), 0.2f, BlockLayer);
    63.             bool BeL_Collided = Physics2D.OverlapCircle(blocks.transform.position + new Vector3(-0.5f, -0.5f), 0.2f, BlockLayer);
    64.             bool BeR_Collided = Physics2D.OverlapCircle(blocks.transform.position + new Vector3(0.5f, -0.5f), 0.2f, BlockLayer);
    65.  
    66.             if(!Ab_Collided && !L_Collided && R_Collided && Be_Collided && BeR_Collided)
    67.             {
    68.                 blocks.GetComponent<SpriteRenderer>().sprite = LTCorner[0];
    69.             }
    70.         }
    71.  
    72.      
    73.     }
    74. }
     
  5. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Why can't you get surrounding tiles from tile grid using their coordinates instead of spherecasting? If that is not possible, you may precalculate different sprite zones bounds as AABBs in the editor script, save it for build as scriptable object or prefab and use that for fast testing what sprite to use.
     
  6. MaxVuksan

    MaxVuksan

    Joined:
    Apr 21, 2019
    Posts:
    5
    I'm assuming when you say from a tile grid you mean the tilemap grid. I'm using game objects not a tilemap. I'm not outstanding at C# could you expand on your idea?
     
  7. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    I'll try. Usually when creating tile based games you have tile grid. It is commonly stored as 2d array or array of arrays of tile instance references or structs with tile description. Also people often using textures for that, encoding tile info into pixels data. This is used for advanced things like rendering entire map on gpu for instance.

    Having this structure set up, I mean tilemap grid, you got me right, you can easily get tile for world position by deviding coordinate on the tile size. After that it becomes super easy to acces your our neihborhood tiles. All tile map engines including the unity's builtin one, have this feature.

    Like if your X = 10 and tile size = 6 then your X' in tiles is 10 / 6 = 1, assuming zero tile is centered in 0,0.

    If you're curious, check this tutors https://catlikecoding.com/unity/tutorials/hex-map/part-1/ . It is about hexagon tiles, square a way much simpler.