캐주얼 슈팅 액션 대전 게임 (모바일) - 11 피격시 카메라 흔들림, Hit Effect 변경
어제는 몇가지 변경 및 추가 사항들이 있었다. 기존의 hitEfffect는 거의 티가 나지 않고 예쁘지가 않아 괜찮은게 있나 찾아보던 중 현재 스타일과 잘 맞는 이펙트를 찾게 되었다. 기존의 코드에서는 그냥 캐릭터의 위치를 받아서 y 값으로 0.5만큼 올렸었다.
하지만 이럴 경우 문제가 있는데 이펙트가 캐릭터 안에서 터지기 때문에 조금 조치를 취해 줄 필요가 있었다.
문제 발견: 이펙트가 캐릭터 안에서 나오기 때문에 눈에 띄지 않는다.
public class EffectManager : MonoBehaviour
{
private MemoryPool memoryPool;
private Camera mainCam;
void Start()
{
memoryPool = GameObject.Find("MemoryPool").GetComponent<MemoryPool>();
mainCam = Camera.main;
}
public void HitEffect(Transform characterPosition)
{
GameObject tempEffect = memoryPool.ActivePoolItem(MemoryPool.ObjectType.HitEffect);
tempEffect.transform.position = characterPosition.position + new Vector3(0, 0.5f, 0);
}
}
해결
public void HitEffect(Transform characterPosition)
{
GameObject tempEffect = memoryPool.ActivePoolItem(MemoryPool.ObjectType.HitEffect);
tempEffect.transform.position = characterPosition.position + new Vector3(0, 0.5f, 0);
Vector3 tempVec = new Vector3(0, 0, 0);
tempVec = (mainCam.transform.forward - tempEffect.transform.forward) * -1.2f;
tempEffect.transform.position += tempVec;
}
처음에 생각했던 방법은 캐릭터 자식으로 빈 게임 오브젝트를 만들어서 그곳에 hitEffect를 발생시키는 것이었다. 하지만 조금 더 생각해보니 이렇게 할 경우 캐릭터가 회전할 때 자식인 빈 게임 오브젝트 역시 회전하기 때문에 이대로 구현 했다면 캐릭터가 앞쪽이나 뒤쪽 을 바라봤을 때 둘 중 한쪽만 제대로 나왔을 것이다.
그래서 카메라 방향을 구한 다음 그 방향으로 가까이 간다면 캐릭터가 어떻게 움직여도 잘 보일 것이라고 생각 해서 maincam.transform.forwad - tempEffect.transform.forwad 를 한 후 방향을 구했으니 거기에 -1.2를 곱했다.
HitEffect 수정 전
HitEffect 수정 후
그리고 자세히 보면 맞을 때마다 카메라가 조금 흔들리는 걸 볼 수 있다. 이 효과는 적을 때릴 때도 나타나게 했다.
흔들림의 세기를 조금만 더 줘도 피로도가 급격히 올라가는 느낌이 들었기 때문에 이정도로 약하게 주는 것이 좋을 것 같다.
public void OnShakeCamera(float shakeTime = 0.05f, float shakeIntensity = 0.05f)
{
if (!playerMediator.PV.IsMine)
return;
this.shakeTime = shakeTime;
this.shakeIntensity = shakeIntensity;
StopCoroutine("ShakeByPosition");
StartCoroutine("ShakeByPosition");
}
public IEnumerator ShakeByPosition()
{
Vector3 startPosition = transform.position;
while (shakeTime > 0.0f)
{
transform.position = startPosition + Random.insideUnitSphere * shakeIntensity;
shakeTime -= Time.deltaTime;
yield return null;
}
transform.position = startPosition;
}
카메라 흔들림 출처: 고박사의 유니티 노트
https://www.youtube.com/watch?v=0El8t9-4x6E