# Unity3D Smooth Moves

Xander here.

For our new game Endless Elevator I’ve been working on finding the best method of moving the character through the scenes.

At it’s most basic our character needs to move left and right across the screen and forwards and backwards on a couple of limited parallel z axis.

The only way he can move up or down is by an escalator or an elevator so we have to take control away from the user in that case and handover to another controller.  Then once we were done moving we can hand control back to the user.

But it was mostly the left-right, forwards-backwards movements I was concerned about for this set of tests.

I wanted a really simple set of controls and didn’t want to hand them over to the Physics Engine with Rigidbodies and Colliders.  But the character movement couldn’t feel stiff or mechanical. We wanted a bit of slack in the movement and bounce .

I scripted up three different methods of controlling the character with flip switches to move between them during testing so we could decide on the best looking movement.

Method One was the most simple basic Vector move.

Method Two was a damped Vector move using mathf.DampSmooth.

Method Three was to implement the Character Controller and use Object.Move.

I’ll put the script down the bottom of the post. Right now I want to look the actual movement controllers and the Input into those commands.

We define our speed as a float and get the x and z values from our Vertical and Horizontal Input Axis (WASD or Arrow Keys on the computer).

public float speed = 10f;
var x = Input.GetAxis(“Horizontal”);     // this is a float value between -1 and 1 that tracks left and right input from the left and right controller.  If it’s under 0 we move left. If it’s over 0 we move right. Simple easy to understand and doesn’t factor into the movement at all except to define which way we go.

var z = Input.GetAxis(“Vertical”);  // We do the same for the z value which governs character depth on the screen (forwards and backwards).

if (x < 0) {
transform.position -= new Vector3(speed * Time.deltaTime, 0, 0);  // Move Left the value of speed and normalised for frame rate differences with the deltaTime function.
}
if (x > 0) {
transform.position += new Vector3(speed * Time.deltaTime, 0, 0);  // Right
}
if (z < 0) {
transform.position -= new Vector3(0, 0, speed * Time.deltaTime);  // Towards the camera
}
if (z > 0)
{
transform.position += new Vector3(0, 0, speed * Time.deltaTime);  // Away from the camera
}
Every Update we move our character in the desired direction at a speed of 10.

This is fine. It works. But it looks kind of static.  The character hits full speed straight away and stops on a dime.  It’s not particularly realistic looking (and maybe you want your game to have this look) and not what I really wanted.  But it’s a great comparison to judge the other two methods by.

Let’s look at the Smooth Damp method next.

We use the same kind of input:  eg. x = Input.GetAxis(“Horizontal”);

speed = 1f;  //  This method is much faster so we need to reduce the speed to keep all the methods at roughly the same rate.

We set some variables that control the “bounciness” of the smoothing.

public float forwardVelocity = 0;
public float sidewaysVelocity = 10;

// These Velocity floats hold the value of the velocity between Updates (I only wanted some on the left right movement)

public float movementSmoothing = 0.15f;  // the higher the number the slower it get’s to top speed.

Mathf.SmoothDamp takes the current position and the raw input from the Input keys. That means that those values between -1 and 1 amp up or down over a small amount of time (depending on the direction) . This is is factored together with the current velocity
movementInput.x = Mathf.SmoothDamp(movementInput.x, Input.GetAxisRaw(“Horizontal”), ref forwardVelocity, movementSmoothing);
movementInput.y = Mathf.SmoothDamp(movementInput.y, Input.GetAxisRaw(“Vertical”), ref sidewaysVelocity, movementSmoothing);
transform.position += new Vector3(movementInput.x * speed, 0, movementInput.y * speed);

//transform.position += new Vector3(movementInput.x * speed * Time.deltaTime, 0, movementInput.y* speed * Time.deltaTime);  // I had that last line with the deltaTime in it as well but it looked “moochy” if that’s a word and did the opposite of smoothing the transitions so it ended up looking like this:

movementInput.x = Mathf.SmoothDamp(movementInput.x, Input.GetAxisRaw(“Horizontal”), ref forwardVelocity, movementSmoothing);

Mathf.SmoothDamp
Gradually changes a value towards a desired goal over time.

public static float SmoothDamp(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed = Mathf.Infinity, float deltaTime = Time.deltaTime);

Input.GetAxisRaw(“Horizontal”) goes straight from -1 on left arrow back to 0 when off and +1 on right arrow – no smoothing.
Input.GetAxis(“Horizontal”); does the same thing but increments gradually over a short time so that it goes from 0 to 1 in a few microseconds.

Sooo using the Raw one and pumping it into the SmoothDamp function does pretty much the same thing. In the Editor I cannot tell the difference between them.

This controller took a bit more playing with but was really looking good.  It picks up speed incrementally when you start moving and there is a controllable amount of exaggerated overshooting when you change direction. If you set the smoothing or velocity too high it gets really bouncy and a little crazy but at low levels it looks cartoony and mimics the  stretch and squash of that genre.

Lastly I looked at the character controller. It’s part of the system so really easy to set up in a simple move script to get a feel for it.

speed = 20f;  // Once again the speed was modified to get a realistic similarity between the three methods.

The character direction comes straight from the input controllers and the cc.Move method handles everything else.

direction = new Vector3((Input.GetAxis(“Vertical”)), 0, (-(Input.GetAxis(“Horizontal”))));
direction1 = transform.TransformDirection(-direction);
cc.Move(direction1 * speed * Time.deltaTime);

Initially this looked really good. But the more I played with it to try and limit the way it was moving the character and the amount of radians it could turn it quickly got way too complicated and I started getting weird movement behaviour.  So if you want a character controller that you can just plug in and go with then this is good. But if you want more control be prepared for a longer haul.

As with everything this was a greats study into character control methods and one step in the path of looking for a smooth moving character but in the end following a massive redesign of the level system of my game I ended up going with a physics based collider system for the levels and a physics controller.

Not all my time wasted though as I had a better understanding of what I wanted and used elements of both the Smooth Move method and the Basic method in my final Character Control script.

This is the final edit of the script I was using if you want to play with it.  Just attach it to a player and add a character controller.  Use the tick boxes on the bools in the editor to switch between methods while playing.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SmoothMove : MonoBehaviour
{

public bool basicMove;
public bool smoothDampMove;
public bool CCMove;

public Vector2 movementInput = Vector2.zero;
public float forwardVelocity = 0;
public float sidewaysVelocity = 10;

public float movementSmoothing = 0.15f; // the higher the number the slower it get’s to top speed
public float speed;

public CharacterController cc;
public Vector3 direction = Vector3.zero;
public Vector3 direction1; // used for debugging in the editor
float smooth = 50.0f;

// Update is called once per frame
void Update()
{
if (basicMove)
{
speed = 10f;
var x = Input.GetAxis(“Horizontal”);
var z = Input.GetAxis(“Vertical”);

if (x < 0)
{
transform.position -= new Vector3(speed * Time.deltaTime, 0, 0);
}
if (x > 0)
{
transform.position += new Vector3(speed * Time.deltaTime, 0, 0);
}

if (z < 0)
{
transform.position -= new Vector3(0, 0, speed * Time.deltaTime);
}
if (z > 0)
{
transform.position += new Vector3(0, 0, speed * Time.deltaTime);
}
}

if (smoothDampMove)
{
speed = 1f;
movementInput.x = Mathf.SmoothDamp(movementInput.x, Input.GetAxisRaw(“Horizontal”), ref forwardVelocity, movementSmoothing);
movementInput.y = Mathf.SmoothDamp(movementInput.y, Input.GetAxisRaw(“Vertical”), ref sidewaysVelocity, movementSmoothing);
//transform.position += new Vector3(movementInput.x * speed * Time.deltaTime, 0, movementInput.y* speed * Time.deltaTime);
transform.position += new Vector3(movementInput.x * speed, 0, movementInput.y * speed);
}

if (CCMove)
{
speed = 20f;
direction = new Vector3((Input.GetAxis(“Vertical”)), 0, (-(Input.GetAxis(“Horizontal”))));
//direction = new Vector3(0, 0, (-Input.GetAxis(“Horizontal”)));
direction1 = transform.TransformDirection(direction);
//direction1 *= speed;
cc.Move(direction1 * speed * Time.deltaTime);
}
}
}