informa
2 MIN READ
Blogs

Snake Ultimate - Dev Blog #1 - Smooth Movement in Unity3D

Today I show you how I achieved aesthetically pleasing grid-based movement in Snake Ultimate.

Snappy movement in arcade games are a big problem! It's aesthetically unpleasing today! 

So, I'm going to show you a super simple way to get your snake moving like its 2015. And the method I'm using for the mobile friendly, Snake Ultimate. 

 

 

 

 

Doing a smooth transition in Unity is easy with Vector.MoveTowards in your Update loop: 
 

 


float speed = 2.0f * Time.deltaTime; 


// move the head to its target location 

tail[0].transform.localPosition = Vector2.MoveTowards ( tail[0].transform.localPosition, targetPos, speed ); 


// move last tile to its target position 

if (tail.Count>0) { 

        int last = tail.Count-1; 

        tail[last].transform.localPosition = Vector2.MoveTowards ( 

                 tail[last].transform.localPosition, tail[last].targetPos, speed ); 

} 

 

 

The target positions are aligned to the grid, so we're STILL snapping the snake to the grid but we're moving it slowly there first. When the head reaches the target position, we set a new position for the tail and the head (aka a tick). This new position is found by looking at the next tile on the grid in front of the head (depending on the direction) : 

 


// set the head's new target position
int targetRow = tail[0].row + dir.y; 

int targetCol = tail[0].col + dir.x; 
 
targetPos = new Vector2 ( targetCol * tileSize, targetRow * tileSize ); 

 
At each tick (which is when the head reaches its target position), the last tile of the snake is moved to the head's position:
 


// move the last tile to the head's position 

if (tail.Count > 0) { 

     tail[tail.Count - 1].transform.localPosition = tail[0].transform.localPosition; 

     tail.Insert(0, tail[tail.Count - 1]); 

     tail.RemoveAt(tail.Count - 1); 

}

 

 


 

You can also choose to use 2d Toolkit's Tiled Sprite to scale the front and end tiles instead of moving them. You'd need to rotate the tile or set an anchor point in the correct direction.

 

 

 

Latest Jobs

Manticore Games

San Mateo, California
8.23.22
Senior Software Engineer - Mobile

Sony PlayStation

San Diego, California
6.23.22
Sr. Online Programmer

The Walt Disney Company

Glendale, California
8.1.22
Associate Marketing Manager - Walt Disney Games

Insomniac Games

Burbank, California
8.26.22
Accessibility Design Researcher
More Jobs   

CONNECT WITH US

Explore the
Subscribe to
Follow us

Game Developer Job Board

Game Developer Newsletter

@gamedevdotcom

Explore the

Game Developer Job Board

Browse open positions across the game industry or recruit new talent for your studio

Browse
Subscribe to

Game Developer Newsletter

Get daily Game Developer top stories every morning straight into your inbox

Subscribe
Follow us

@gamedevdotcom

Follow us @gamedevdotcom to stay up-to-date with the latest news & insider information about events & more