Last active
January 3, 2016 15:39
-
-
Save arket/8484545 to your computer and use it in GitHub Desktop.
即席スクリプト。
Cameraオブジェクトにセットすれば、CatchShakeメソッドをコールした時、地震みたくなる。
移動しながらの振動はNG。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class ShakeCamera : MonoBehaviour { | |
public float setShakeTIme; // 持続振動時間 | |
private float lifeTime; | |
private Vector3 savePosition; | |
private float lowRangeX; | |
private float maxRangeX; | |
private float lowRangeY; | |
private float maxRangeY; | |
void CatchShake() { | |
savePosition = transform.position; | |
lowRangeY = savePosition.y - 1.0f; | |
maxRangeY = savePosition.y + 1.0f; | |
lowRangeX = savePosition.x - 1.0f; | |
maxRangeX = savePosition.x + 1.0f; | |
lifeTime = setShakeTIme; | |
} | |
void Start () { | |
if(setShakeTIme <= 0.0f) | |
setShakeTIme = 0.7f; | |
lifeTime = 0.0f; | |
} | |
void Update () { | |
if(lifeTime < 0.0f){ | |
transform.position = savePosition; | |
lifeTime = 0.0f; | |
} | |
if(lifeTime > 0.0f){ | |
lifeTime -= Time.deltaTime; | |
float x_val = Random.Range(lowRangeX,maxRangeX); | |
float y_val = Random.Range(lowRangeY,maxRangeY); | |
transform.position = new Vector3(x_val,y_val,transform.position.z); | |
} | |
if(Input.GetKeyDown("space")) | |
CatchShake(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment