ダメージで視界を揺らす
視界を揺らす
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraShake : MonoBehaviour
{
public void Shake(float duration, float magnitude)
{
StartCoroutine(TheShake(duration, magnitude));
}
private IEnumerator TheShake(float duration, float magnitude)
{
Vector3 pos = transform.localPosition;
float elapsed = 0f; // 経過時間
// duration・・・>どのくらいの時間揺らすかをコントロール
while(elapsed < duration)
{
// magnitude・・・>どのくらいの大きさで揺らすかをコントロール
var x = pos.x + Random.Range(-1f, 1f) * magnitude;
var y = pos.y + Random.Range(-1f, 1f) * magnitude;
transform.localPosition = new Vector3(x, y, pos.z);
elapsed += Time.deltaTime;
yield return null;
}
transform.localPosition = pos;
}
}
視界を揺らす
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerHP : MonoBehaviour
{
public int maxHP;
public AudioClip sound;
public int currentHP;
// ★追加(視界を揺らす)
public CameraShake camShake;
private void Start()
{
currentHP = maxHP;
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if(hit.gameObject.CompareTag("BombTrap"))
{
currentHP -= 1;
AudioSource.PlayClipAtPoint(sound, transform.position);
Destroy(hit.gameObject);
// ★追加(視界を揺らす)
camShake.Shake(0.2f, 0.1f);
if (currentHP < 1)
{
Invoke("GoToGameover", 1.5f);
}
}
}
void GoToGameover()
{
SceneManager.LoadScene("GameOver");
}
}
【2019版】X_Mission(基礎/全51回)
他のコースを見る視界を揺らす
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraShake : MonoBehaviour
{
public void Shake(float duration, float magnitude)
{
StartCoroutine(TheShake(duration, magnitude));
}
private IEnumerator TheShake(float duration, float magnitude)
{
Vector3 pos = transform.localPosition;
float elapsed = 0f; // 経過時間
// duration・・・>どのくらいの時間揺らすかをコントロール
while(elapsed < duration)
{
// magnitude・・・>どのくらいの大きさで揺らすかをコントロール
var x = pos.x + Random.Range(-1f, 1f) * magnitude;
var y = pos.y + Random.Range(-1f, 1f) * magnitude;
transform.localPosition = new Vector3(x, y, pos.z);
elapsed += Time.deltaTime;
yield return null;
}
transform.localPosition = pos;
}
}
視界を揺らす
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerHP : MonoBehaviour
{
public int maxHP;
public AudioClip sound;
public int currentHP;
// ★追加(視界を揺らす)
public CameraShake camShake;
private void Start()
{
currentHP = maxHP;
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if(hit.gameObject.CompareTag("BombTrap"))
{
currentHP -= 1;
AudioSource.PlayClipAtPoint(sound, transform.position);
Destroy(hit.gameObject);
// ★追加(視界を揺らす)
camShake.Shake(0.2f, 0.1f);
if (currentHP < 1)
{
Invoke("GoToGameover", 1.5f);
}
}
}
void GoToGameover()
{
SceneManager.LoadScene("GameOver");
}
}
ダメージで視界を揺らす