Need help with script

Help me, please, i can’t write script: when Player don’t move, playing animation “Relax”, when his speed > 0, playing animation “Walking”
My script doesn’t work :frowning:

You should post your script! Anyway, you can always measure the actual character speed in Update, and use it to play your animations:

private var lastPos: Vector3;
private var curVel: float = 0;

function Start(){
  lastPos = transform.position; // initialize lastPos
}

function Update(){
  // calculate displacement from lastPos:
  var moved: Vector3 = transform.position - lastPos;
  lastPos = transform.position; // update lastPos
  // calculate current velocity:
  curVel = moved.magnitude/Time.deltaTime;
  // use current velocity to select the animation:
  if (curVel < 0.2){
    animation.CrossFade("Relax");
  } else {
    animation.CrossFade("Walking");
  }
}

This is the basic idea: always keep the position in the last frame in lastPos and use it to calculate the velocity. This approach works fine, but you may have two problems:

1- If you pause your game with Time.timeScale = 0, Time.deltaTime may return 0 and fill your console with error messages;

2- The vertical velocity is taken into account, thus the character may “walk in the air” when falling (weird effect);

The improved version below solves both problems with minor changes;

private var lastPos: Vector3;
private var curVel: float = 0;

function Start(){
  lastPos = transform.position; // initialize lastPos
}

function Update(){
  // calculate displacement from lastPos:
  var moved: Vector3 = transform.position - lastPos;
  lastPos = transform.position; // update lastPos
  // kill the vertical velocity in order to not walk in the air:
  moved.y = 0;
  // only calculate current velocity if Time.deltaTime is non 0:
  if (Time.deltaTime > 0) curVel = moved.magnitude/Time.deltaTime;
  // use current velocity to select the animation:
  if (curVel < 0.2){
    animation.CrossFade("Relax");
  } else {
    animation.CrossFade("Walking");
  }
}