patterncsharpMinor
Slow update function
Viewed 0 times
functionupdateslow
Problem
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();
}
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 0 && angularSpeed >= 0f)
angularSpeed = Mathf.Lerp (angularSpeed, 2.5f axisX + angularSpeedOffset, turnTime Time.deltaTime);
else if (axisX 0.2f)
angu
```
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();
}
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 0 && angularSpeed >= 0f)
angularSpeed = Mathf.Lerp (angularSpeed, 2.5f axisX + angularSpeedOffset, turnTime Time.deltaTime);
else if (axisX 0.2f)
angu
Solution
You have a lot of magic numbers in your code which should be extracted to meaningful named constants.
...
Using braces
Unneeded checks should be omitted too like
should be
Don't shorten your variable names because it reduces readability. E.g
should be
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;Code Snippets
if (Mathf.Abs (angularSpeed) > 0.2f)
if (angularSpeed >= 2.5f) angularSpeed = 2.5f;if (angularSpeed >= 2.5f) angularSpeed = 2.5f;
else if (angularSpeed <= -2.5f) angularSpeed = -2.5f;if (angularSpeed > 2.5f)
{
angularSpeed = 2.5f;
}
else if (angularSpeed < -2.5f)
{
angularSpeed = -2.5f;
}private Animator anim;private Animator animator;Context
StackExchange Code Review Q#80738, answer score: 2
Revisions (0)
No revisions yet.