Skip to content

Instantly share code, notes, and snippets.

@HassakuTb
Created January 1, 2017 07:35
Show Gist options
  • Save HassakuTb/bd577ecde2c3ca9a8fbd21f72ae9e878 to your computer and use it in GitHub Desktop.
Save HassakuTb/bd577ecde2c3ca9a8fbd21f72ae9e878 to your computer and use it in GitHub Desktop.
震える
/**
NYSL Version 0.9982 (en) (Unofficial)
----------------------------------------
A. This software is "Everyone'sWare". It means:
Anybody who has this software can use it as if he/she is
the author.
A-1. Freeware. No fee is required.
A-2. You can freely redistribute this software.
A-3. You can freely modify this software. And the source
may be used in any software with no limitation.
A-4. When you release a modified version to public, you
must publish it with your name.
B. The author is not responsible for any kind of damages or loss
while using or misusing this software, which is distributed
"AS IS". No warranty of any kind is expressed or implied.
You use AT YOUR OWN RISK.
C. Copyrighted to Hassaku
D. Above three clauses are applied both to source and binary
form of this software.
*/
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class Shaker : MonoBehaviour {
private Vector2 offset = Vector2.zero;
private float angleOffset = 0.0f;
[HideInInspector]
public bool isPlaying;
/// <summary>
/// positionによる振動を有効にするか
/// </summary>
public bool enablePositionShake = true;
/// <summary>
/// 振動の大きさ
/// </summary>
public float distance = 1.0f;
/// <summary>
/// 回転による振動を有効にするか
/// </summary>
public bool enableAngleShake = true;
/// <summary>
/// 回転角の最大値([0.0f -- 180.0f])
/// </summary>
[Range(0.0f, 180.0f)]
public float angle = 15.0f;
/// <summary>
/// awake時に振動を開始するか
/// </summary>
public bool startOnAwake = false;
void Awake () {
if (startOnAwake) StartShake();
}
void Update () {
if (isPlaying) {
ResetPosition();
if (enablePositionShake) {
float moveAngle = Random.Range(0.0f, Mathf.PI * 2.0f);
offset = new Vector2(distance * Mathf.Cos(moveAngle), distance * Mathf.Sin(moveAngle));
transform.position = transform.position.AddVector2(offset);
}
if (enableAngleShake) {
float rotateAngle = Random.Range(-angle, angle);
angleOffset = rotateAngle;
transform.Rotate(0.0f, 0.0f, angleOffset);
}
}
}
/// <summary>
/// 振動の開始
/// </summary>
public void StartShake() {
isPlaying = true;
}
/// <summary>
/// 振動の停止
/// </summary>
public void StopShake() {
ResetPosition();
isPlaying = false;
}
private void ResetPosition() {
transform.position = transform.position.AddVector2(-offset);
offset = Vector2.zero;
transform.Rotate(0.0f, 0.0f, -angleOffset);
angleOffset = 0.0f;
}
#region Inspector拡張
#if UNITY_EDITOR
[CustomEditor(typeof(Shaker))]
public class ShakerEditor : Editor {
public override void OnInspectorGUI() {
base.OnInspectorGUI();
Shaker shaker = target as Shaker;
if (shaker.isPlaying) {
if (GUILayout.Button("Stop")) {
shaker.StopShake();
}
}
else {
if (GUILayout.Button("Start")) {
shaker.StartShake();
}
}
}
}
#endif
#endregion
}
static class Vector3Extensions {
public static Vector3 AddVector2(this Vector3 target, Vector2 v) {
return new Vector3(target.x + v.x, target.y + v.y, target.z);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment