본문 바로가기
Project/[Unity] 지진 시뮬레이션

[Unity] 지진 시뮬레이션 - 2 (지진 구현)

by ruki 2024. 3. 19.

배경을 제작하였으므로

지진 시뮬레이션에서 가장 중요한 지진을 구현할 것이다.


앞서 카메라 흔들림 효과 코드를 이용하여 지진을 구현해 보았으나 위치값만 수정하면 마찰이 적용되지 않는 것 같았다.

그래서 위치값이 아닌 물리값을 수정하게 만들어보았다.

1. 바닥에 물리 적용

바닥 오브젝트에 Rigidbody를 적용하였다.

 

그러자 "Non-convex MeshCollider with non-kinematic Rigidbody is no longer supported since Unity 5."라는 오류가 발생하였다.

그래서 바닥을 박스 오브젝트로 다시 생성하였더니 오류가 해결되었다.

 

그리고 원하는 움직임을 위해서 바닥의 위치와 회전을 고정시켰다.

2. 움직임을 물리로 구현

일반적인 position같은 위치값을 직접 변환시키는 코드는 물리의 마찰이 적용되지 않는 것 같았다.

그래서 속도 값인 velocity를 직접 수정해주니 훌륭하게 지진이 구현되었다.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class earthquakeCode : MonoBehaviour
{
    public bool play;

    public float shakeSpeed = 2.0f;
    public float shakeAmount = 1.0f;
 
    Rigidbody rb;

    Vector3 originPosition;
    
    void Start()
    {
        rb = gameObject.GetComponent<Rigidbody>();
        originPosition = transform.localPosition;
    }

    void Update()
    {
        if (play)
        {
            float elapsedTime = 0.0f;

            float x = Mathf.Cos(Time.time * 360 * Mathf.Deg2Rad * shakeSpeed)*shakeAmount;

            rb.velocity = new Vector3(x, 0, 0);
            }
    }
}