本文摘自蛮牛http://www.manew.com/thread-45376-1-1.html
依次是:缓动、间歇、加速运动、摩擦力、弹性移动、椭圆运动、圆周运动、正弦余弦运动
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
using UnityEngine; using System.Collections; using System; public class TestMoveAnimation : MonoBehaviour { public enum MoveType { None, Yuan, TuoYuan, HuanDong, JianXie, ZhengCibo, YuSu, JiaSu, MoCaLi, JiaoJiaSu, TanXing, } public Transform ball; public Transform target; public MoveType moveType = MoveType.None; public Vector3 center = Vector3.zero; public float dis = 3f; private float _px = 0f; private float _pz = 0f; private float _py = 0f; private float _angele = 0f; private float _ratio = 0.05f; private float _moveSpeed = 0.05f; private float _a = 0.001f; private float _ax = 0.001f; private float _az = 0.001f; private float _moveSpeedx = 0.05f; private float _moveSpeedz = 0.01f; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (ball != null && target != null) { if (moveType == MoveType.Yuan) { _yuanMove(); _toBounds(); } if (moveType == MoveType.TuoYuan) { _tuoYuanMove(); _toBounds(); } if (moveType == MoveType.JianXie) { _JianXieMove(); _toBounds(); } if (moveType == MoveType.ZhengCibo) { _ZhengCiBoMove(); _toBounds(); } if (moveType == MoveType.YuSu) { _YuSuMove(); _toBounds(); } if (moveType == MoveType.JiaSu) { _JiaSuMove(); _toBounds(); } if (moveType == MoveType.MoCaLi) { _MoCaLiMove(); _toBounds(); } if (moveType == MoveType.HuanDong) { _huanDongMove(); _toBounds(); } if (moveType == MoveType.TanXing) { _tanXinMove(); } } } void OnGUI() { if (GUILayout.Button("圆周运动")) { _angele = 0f; _moveSpeed = .2f; ball.localPosition = Vector3.zero; target.localPosition = new Vector3(18f, 0f, 0f); moveType = MoveType.Yuan; target.gameObject.SetActive(false); } if (GUILayout.Button("椭圆运动")) { _angele = 0f; _moveSpeed = .2f; ball.localPosition = Vector3.zero; target.localPosition = new Vector3(18f, 0f, 0f); moveType = MoveType.TuoYuan; target.gameObject.SetActive(false); } if (GUILayout.Button("简谐振动")) { _angele = 0f; _moveSpeed = .2f; ball.localPosition = Vector3.zero; target.localPosition = new Vector3(18f, 0f, 0f); moveType = MoveType.JianXie; target.gameObject.SetActive(false); } if (GUILayout.Button("正弦余弦运动")) { _angele = 0f; _moveSpeed = .25f; ball.localPosition = new Vector3(-18f, 0f, 0f); target.localPosition = new Vector3(18f, 0f, 0f); moveType = MoveType.ZhengCibo; target.gameObject.SetActive(false); } if (GUILayout.Button("匀速运动")) { _angele = 0f; _moveSpeed = .2f; ball.localPosition = new Vector3(-18f, 0f, 0f); target.localPosition = new Vector3(18f, 0f, 0f); moveType = MoveType.YuSu; target.gameObject.SetActive(true); } if (GUILayout.Button("加速运动")) { _angele = 0f; _moveSpeed = .5f; _a = 0.001f; ball.localPosition = new Vector3(-18f, 0f, 0f); target.localPosition = new Vector3(18f, 0f, 0f); moveType = MoveType.JiaSu; target.gameObject.SetActive(true); } if (GUILayout.Button("摩擦力")) { _angele = 0f; _moveSpeed = 3f; ball.localPosition = new Vector3(-18f, 0f, 0f); target.localPosition = new Vector3(18f, 0f, 0f); moveType = MoveType.MoCaLi; target.gameObject.SetActive(false); } if (GUILayout.Button("缓动")) { _angele = 0f; _moveSpeed = 2f; ball.localPosition = new Vector3(-18f, 0f, 0f); target.localPosition = new Vector3(18f, 0f, 0f); moveType = MoveType.HuanDong; target.gameObject.SetActive(true); } if (GUILayout.Button("弹性移动")) { _angele = 0f; _moveSpeed = 2f; ball.localPosition = new Vector3(-18f, 0f, 0f); target.localPosition = Vector3.zero; moveType = MoveType.TanXing; target.gameObject.SetActive(true); } } private void _yuanMove() { _angele += 0.1f; _px = center.x + dis * (float)Math.Cos(_angele); _pz = center.z + dis * (float)Math.Sin(_angele); _py = 0f; ball.localPosition = new Vector3(_px, _py, _pz); } private void _tuoYuanMove() { _angele += 0.1f; _px = center.x + dis * (float)Math.Cos(_angele); _pz = center.z + dis * 0.5f * (float)Math.Sin(_angele); _py = 0f; ball.localPosition = new Vector3(_px,_py, _pz); } private void _huanDongMove() { float dis = Vector3.Distance(ball.localPosition, target.localPosition); float speed = dis * _ratio; _px = ball.localPosition.x + speed; _py = 0f; _pz = 0f; ball.localPosition = new Vector3(_px, _py, _pz); } private void _JianXieMove() { _angele += 0.1f; // _px = center.x + dis * (float)Math.Cos(_angele); _pz = center.z + dis * (float)Math.Sin(_angele); _py = 0f; _px = 0f; ball.localPosition = new Vector3(_px, _py, _pz); } private void _ZhengCiBoMove() { _angele += 0.15f; _px = ball.localPosition.x + _moveSpeed; _pz = center.z + dis * (float)Math.Sin(_angele); _py = 0f; ball.localPosition = new Vector3(_px,_py, _pz); } private void _YuSuMove() { float dis = Vector3.Distance(ball.localPosition, target.localPosition); if (dis > 0.5f) { _px =ball.localPosition.x + _moveSpeed; _py = 0f; _pz = 0f; ball.localPosition = new Vector3(_px, _py, _pz); } } private void _JiaSuMove() { float dis = Vector3.Distance(ball.localPosition, target.localPosition); if (dis > 0.5f) { _moveSpeed += _a; _px = ball.localPosition.x + _moveSpeed; _py = 0f; _pz = 0f; ball.localPosition = new Vector3(_px, _py, _pz); } else { } } private void _MoCaLiMove() { if (_moveSpeed >= 0.001f) _moveSpeed *= 0.92f; else _moveSpeed = 0f; _px = ball.localPosition.x + _moveSpeed; _py = 0f; _pz = 0f; ball.localPosition = new Vector3(_px, _py, _pz); } private void _tanXinMove() { float disx = target.localPosition.x - ball.localPosition.x ; float disz = target.localPosition.z - ball.localPosition.z; _ax = disx * 0.1f; _az = disz * 0.1f; _moveSpeedx += _ax; _moveSpeedz += _az; _moveSpeedx *= 0.95f; _moveSpeedz *= 0.95f; _px = ball.localPosition.x + _moveSpeedx; _py =0f; _pz =ball.localPosition.z + _moveSpeedz; ball.localPosition = new Vector3(_px, _py, _pz); if (Math.Abs(_moveSpeedx) <= 0.0001f && Math.Abs(_moveSpeedz) <= 0.0001f) { target.localPosition = new Vector3(UnityEngine.Random.Range(-18f, 18f), 0f,UnityEngine.Random.Range(-10f, 10f)); } } private void _toBounds() { if (ball.localPosition.x > 25f) { ball.localPosition = new Vector3(-25f,ball.localPosition.y,ball.localPosition.z); } else if (ball.localPosition.x < -25f) { ball.localPosition = new Vector3(25f, ball.localPosition.y, ball.localPosition.z); } if (ball.localPosition.z >15f) { ball.localPosition = new Vector3(ball.localPosition.x, ball.localPosition.y,-15f); } else if (ball.localPosition.z <-15f) { ball.localPosition = new Vector3(ball.localPosition.x, ball.localPosition.y,15f); } } } |
- 本文固定链接: http://www.u3d8.com/?p=530
- 转载请注明: 网虫虫 在 u3d8.com 发表过