# Slow update function

I've been fiddling with Unity, trying to make nice walking/running animations. So, i found the Stealth tutorial and I used pretty much the same animator as the AI, only I only use Speed and AngularSpeed and, of course, added a control script. The controls work as intended but the whole script is laggy. I know, my Update function is really loaded, but I'm out of ideas. Any input appreciated even if it is totally irrelevant :)

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {

public float speed = 0f;        //used public just to check the values

public float angularSpeed = 0f; //same

public float accTime = 1f;
public float decTime = 1f;
public float turnTime = 2.5f;

public float speedOffset = 0f;      //offsets set to let lerp "go faster" when approaching the target value
public float angularSpeedOffset = 0f;   //same

private float axisX = 0f;
private float axisY = 0f;

private int speedHash = Animator.StringToHash ("Speed");
private int angularSpeedHash = Animator.StringToHash ("AngularSpeed");

private Animator anim;

void Awake () {
anim = GetComponent<Animator>();
}

void Update () {
SpeedCalc ();
AngularSpeedCalc ();

anim.SetFloat (speedHash, speed);
anim.SetFloat (angularSpeedHash, angularSpeed);
}

void SpeedCalc () {
axisY = Input.GetAxis ("Vertical");

if (axisY > 0)
speed = Mathf.Lerp (speed, 5.7f * axisY + speedOffset, accTime * Time.deltaTime);       // targets multiplied by axis input for analogue sticks to be... analogue..
else
speed = Mathf.Lerp (speed, 0f - speedOffset * 0.5f , decTime * Time.deltaTime);

if (speed >= 5.7f) speed = 5.7f
else if (speed <= 0f) speed = 0f;
}

void AngularSpeedCalc () {
axisX = Input.GetAxis ("Horizontal");

if (axisX > 0 && angularSpeed >= 0f)
angularSpeed = Mathf.Lerp (angularSpeed, 2.5f * axisX + angularSpeedOffset, turnTime * Time.deltaTime);
else if (axisX < 0 && angularSpeed <= 0f)
angularSpeed = Mathf.Lerp (angularSpeed, 2.5f * axisX - angularSpeedOffset, turnTime * Time.deltaTime);
else {
if (Mathf.Abs (angularSpeed) > 0.2f)
angularSpeed = Mathf.Lerp (angularSpeed, 0f, turnTime * Time.deltaTime);
else
angularSpeed = 0f;
}

if (angularSpeed >= 2.5f) angularSpeed = 2.5f;
else if (angularSpeed <= -2.5f) angularSpeed = -2.5f;
}
}

• I am not getting the reason for Mathf.Lerp here. At least the t parameter should only be able to be between 0 and 1 which in your case isn't guranteed, when Time.deltaTime for some reason is bigger than 1. So i guess the calculation for t can't be right and should be some fraction between an expected time and the deltatime – BoeseB Feb 17 '15 at 9:58
• I used Lerp to get smooth acceleration, turning. Also the general damp*Time.deltaTime is pretty common in the tutorials I have seen. However, I run the script again with a fixed t<1 and still no success. – Teo Alivanoglou Feb 17 '15 at 14:00
• Yeah sorry i searched for the stealth tutorial and looked into their code and also found damp * time.delta beiing used to smooth out the transition of the character turning. Could you describe what you call laggy ab bit more precise? are you just playing the animation with the object always in the same place? Your code seems like it should execute pretty fast so i don't think its a problem of the amount of calculations more like a mistake in the formulas – BoeseB Feb 17 '15 at 14:28
• By laggy I mean it had low fps. However, I tried to make a test build and it worked really well and smoothly. Turns out it was an editor issue. – Teo Alivanoglou Feb 17 '15 at 21:31

You have a lot of magic numbers in your code which should be extracted to meaningful named constants.

if (Mathf.Abs (angularSpeed) > 0.2f)
if (angularSpeed >= 2.5f) angularSpeed = 2.5f;


...

Using braces {} for single statements if or else parts shouldn't be omitted to make your code less error prone.

Unneeded checks should be omitted too like

if (angularSpeed >= 2.5f) angularSpeed = 2.5f;
else if (angularSpeed <= -2.5f) angularSpeed = -2.5f;


should be

if (angularSpeed > 2.5f)
{
angularSpeed = 2.5f;
}
else if (angularSpeed < -2.5f)
{
angularSpeed = -2.5f;
}


Don't shorten your variable names because it reduces readability. E.g

private Animator anim;


should be

private Animator animator;