using UnityEngine using System Collections RequireComponent typeof Ani

 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Animation))]
public class BallController : MonoBehaviour {
private Camera cam;
private AnimationClip clip;
private AnimationEvent evt;
private float easeOutBounce(float start, float end, float val){
val /= 1.0f;
end -= start;
if (val < (1.0f / 2.75f)){
return end * (7.5625f * val * val) + start;
}else if (val < (2.0f / 2.75f)){
val -= (1.5f / 2.75f);
return end * (7.5625f * val * val + 0.75f) + start;
}else if (val < (2.5f / 2.75f)){
val -= (2.25f / 2.75f);
return end * (7.5625f * val * val + 0.9375f) + start;
}else{
val -= (2.625f / 2.75f);
return end * (7.5625f * val * val + 0.984375f) + start;
}
}
private void animationEnded (AnimationEvent e) {
Debug.Log("Animation ended");
}
// Use this for initialization
void Start () {
if (cam == null) {
cam = Camera.main;
}
float screenWidth = cam.ScreenToWorldPoint(new Vector3(Screen.width, 0.0f, 0.0f)).x;
float currentTime = 0.0f;
float startingValue = -screenWidth;
float finalValue = screenWidth;
float duration = 1.0f;
int keyFramesCount = 60;
Keyframe[] keyFrames = new Keyframe[keyFramesCount];
for (int i = 0; i < keyFramesCount; i++) {
currentTime = i * duration / (keyFramesCount - 1);
keyFrames[i] = new Keyframe(currentTime, easeOutBounce(startingValue, finalValue, currentTime / duration));
}
clip = new AnimationClip();
clip.SetCurve("", typeof(Transform), "localPosition.x", new AnimationCurve(keyFrames));
clip.SetCurve("", typeof(Transform), "localPosition.y", new AnimationCurve(new Keyframe(0, 0)));
clip.SetCurve("", typeof(Transform), "localPosition.z", new AnimationCurve(new Keyframe(0, 0)));
evt = new AnimationEvent();
evt.time = 1.0f;
evt.functionName = "animationEnded";
clip.AddEvent(evt);
animation.AddClip(clip, clip.name);
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.A)) {
animation.Play(clip.name);
}
}
}