Skip to content

Instantly share code, notes, and snippets.

@arket
Last active January 3, 2016 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arket/8484545 to your computer and use it in GitHub Desktop.
Save arket/8484545 to your computer and use it in GitHub Desktop.
即席スクリプト。 Cameraオブジェクトにセットすれば、CatchShakeメソッドをコールした時、地震みたくなる。 移動しながらの振動はNG。
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