-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoveTest.cs
48 lines (36 loc) · 1.17 KB
/
MoveTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using UnityEngine;
using System;
using System.Collections;
public class MoveTest : MonoBehaviour
{
//initialization of animationclips
public AnimationClip idleAnimation;
public AnimationClip walkAnimation;
public float WalkMaxAnimationSpeed = 10.0f;
public float influenceVelocityOnSpeed = 1.0f;
Vector3 previousPosition;
public void Start(){
//make all animations loop
animation.wrapMode = WrapMode.Loop;
}
public void Update(){
//Get the difference/distance between the previous position and the current position
float travelledDist = Vector3.Distance(previousPosition, transform.position);
//Calculate Velocity
float velocity = travelledDist / Time.deltaTime;
previousPosition = transform.position;
if(velocity > 0.01f)
{
animation.CrossFade(walkAnimation.name);
animation[walkAnimation.name].speed = velocity*influenceVelocityOnSpeed;
// animation[walkAnimation.name].speed = Mathf.Clamp(velocity*influenceVelocityOnSpeed, WalkMaxAnimationSpeed, 0.0f);
}
else
{
animation.CrossFade(idleAnimation.name);
}
//update location of previous position
//previousPosition = transform.position;
// Debug.Log (velocity);
}
}