Search code examples
c#unity-game-enginegame-enginemeshtile

Mesh tilemap and scrolling


I'm working on a top-down game where the player can move up, down, left, or right. I've come across tilemaps but since using Unity, I'm seeing the idea of using Meshes. I don't quite understand how this works. Is it one mesh (two triangles) per tile? If I want to perform scrolling, is it a case of simply swapping out the UV values per vertex?

In theory, could I get an infinite landscape scrolling by creating one large mesh and simply swap out UV coordinates in realtime or am I misunderstanding the uses of Meshes and UV coordinates on the texture atlas?

Thanks


Solution

  • if you want to scroll uv`s , you can do it by changing the texture offset in Update method , it can be used for background of a 2d game

    using UnityEngine;
    using System.Collections;
    
    public class ScrollingUVs : MonoBehaviour 
    {
        public int materialIndex = 0;
        public Vector2 uvAnimationRate = new Vector2( 1.0f, 0.0f );
        public string textureName = "_MainTex";
    
        Vector2 uvOffset = Vector2.zero;
    
        void LateUpdate() 
        {
            uvOffset += ( uvAnimationRate * Time.deltaTime );
            if( renderer.enabled )
            {
                renderer.materials[ materialIndex ].SetTextureOffset( textureName, uvOffset );
            }
        }
    }
    

    and you can use object scrolling when you want to move an object in plane and when it reaches one end it appears in its initial position like a loop

    public class ScrollingObj : MonoBehaviour 
    {
        float speed ;  
        float resetDistance;  
        float initialDistance;  
        boolean isVertical;  
    
         void Start()
        {
          isVertical=false;
        }
    
    
            void Update ()  
            {  
                float move = speed * Time.deltaTime;  
                if (isVertical) {  
                    transform.Translate(Vector3.down * move, Space.World);  
                    if (transform.position.y < resetDistance)  
                    {  
                        transform.position = Vector3(transform.position.x, initialDistance, transform.position.z);  
                    }  
                }else{  
                    transform.Translate(Vector3.left * move, Space.World);  
                    if (transform.position.x < resetDistance)  
                    {  
                        transform.position = Vector3(initialDistance, transform.position.y, transform.position.z);  
                    }  
                }  
            }  
    }