kidoOooOoooOOom

IT系で開発やってます

[Unity] カメラ揺らす

敵の攻撃を受けた時の効果としてカメラを揺らす場合のスクリプトを作成した。こんな感じ。

public class ShakeCamera : MonoBehaviour
{

    public float shake_decay = 0.002f;
    public float coef_shake_intensity = 0.3f;
    private Vector3 originPosition;
    private Quaternion originRotation;
    private float shake_intensity;

    void Update ()
    {  
        if (shake_intensity > 0) {  
            transform.position = originPosition + Random.insideUnitSphere * shake_intensity;  
            transform.rotation = new Quaternion (
                                             originRotation.x + Random.Range (-shake_intensity, shake_intensity) * 2f,  
                                             originRotation.y + Random.Range (-shake_intensity, shake_intensity) * 2f,  
                                             originRotation.z + Random.Range (-shake_intensity, shake_intensity) * 2f,  
                                             originRotation.w + Random.Range (-shake_intensity, shake_intensity) * 2f);  
            shake_intensity -= shake_decay;  
        }  
    }
    
    public void Shake ()
    {  
        originPosition = transform.position;  
        originRotation = transform.rotation;  
        shake_intensity = coef_shake_intensity;  
    }  

}

こちらのサイトをほぼ参考にしました。
http://www.galawana.com/unity3d-camera-shake/