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

Double Fine Productions

Hybrid, San Francisco CA, USA
10.25.23
Senior Systems Programmer

Purdue University

West Lafayette, IN, USA
11.14.23
Clinical Assistant Professor in Game Development

Digital Extremes

Remote
11.13.23
Lead AI Programmer
More Jobs   

CONNECT WITH US

Explore the
Advertise with
Follow us

Game Developer Job Board

Game Developer

@gamedevdotcom

Explore the

Game Developer Job Board

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

Browse
Advertise with

Game Developer

Engage game professionals and drive sales using an array of Game Developer media solutions to meet your objectives.

Learn More
Follow us

@gamedevdotcom

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