2025년 1월 1일 TIL

John Jean·2025년 1월 2일

Unity

목록 보기
5/19

Solving momentum problems

Lazy Sweeper 할때도 생겼던 똑같은 문제를 해결하는법.

로켓이 다른 콜라이더와 충돌해서 rigid body의 값이 바뀌고 있을 때 플레이어 컨트롤러로 회전이나 이동을 하면 아주 어색한 상황이 연출된다.

캐릭터가 의도하지 않은 곳으로 스스로 이동한다던가, 입력한 키가 먹지를 않는다.

Lazy Sweeper에서 내가 사용한 방법은 RB를 끄고, is kinematic을 켜서 물리적 상호작용이 안되도록 했는데, 훨씬 간단한 방법이 있었다.

rb.freezeRotation = true;
transform.Rotate(Vector3.forward * rotationThisFrame * Time.fixedDeltaTime);
rb.freezeRotation = false;

요따구로 RB를 얼리고, 필요한 연산을 한다음 다시 RB를 풀면 된다. 이걸 메서드화 해서 update 해주면 해결되는 간단한 문제였다.

이 방법은 RB와 연산을 매 프레임마다 실행하고, 내가 사용한 방법은 상태를 한번 변경하기만 해서 성능은 내 방법이 더 좋아보이는데, 이방법이 더 직관적이긴 하네.

Unity Audio

기본적으로 메인카메라에 오디오 리스너가 있음.

음향을 추가하는건 간단함. 스크립트에서 캐싱해주고,

using UnityEngine.Audio;

...

[SerializeField] AudioClip CrashSound;
[SerializeField] AudioClip SuccessSound;

오브젝트의 오디오 소스 컴포넌트를 불러와

AudioSource audioSource;

private void Start()
{
    audioSource = GetComponent<AudioSource>();
}

필요한 곳에서 재생시키면 된다.

audioSource.Stop();
audioSource.PlayOneShot(CrashSound);

ezpz

Scene Manager

씬 매니저를 통해 리스폰, 스테이지 이동 등 장면을 이동할 수 있음.

File - Build Profiles 에서 현재 등록돼있는 씬을 참고할 수 있다. 필요없는건 지워주고, 추가해줄 씬은 추가하면 됨. 오른쪽에 적힌 숫자는 인덱스인데, 몇번째 씬인지 알려주는 숫자다.

간단한 리스폰하는 방법과 스테이지 이동하는 방법을 코드로 첨부. 어렵지 않으니 보고 복기해도 될듯

using UnityEngine.SceneManagement;

...

void MoveToNextLevel()
{
    int currentScene = SceneManager.GetActiveScene().buildIndex;
    if (currentScene + 1 != SceneManager.sceneCountInBuildSettings)
    {
        SceneManager.LoadScene(currentScene+1);
    }
    else
    {
        SceneManager.LoadScene(0);
    }
}
void ReloadLevel()
{
    int currentScene = SceneManager.GetActiveScene().buildIndex;
    SceneManager.LoadScene(currentScene);
}
if (currentScene + 1 != SceneManager.sceneCountInBuildSettings)
    {
        SceneManager.LoadScene(currentScene+1);
    }
    ~~~

이부분이 이제 현재  씬이 마지막 인덱스인 씬인지 검사하는 부분이다. `SceneManager.sceneCountInBuildSettings` 이거 쓰면 됨.


## Using Invoke

Using *Invoke()* allows us to call a method so it executes after a delay of x seconds.

`Invoke("MethodName", delayInSeconds);`

충돌 후 바로 리스폰하는게 아니라 효과음이나 연출을 주고싶을 때 사용하면 좋을듯.


## Particles

간단하게 파티클 시스템을 구현하는 방법.

하이어라키 우클릭 - 이펙트 - 파티클 시스템 or 컴포넌트에서 파티클시스템 추가.

파티클에 관한 세부 설정은 직관적으로 되어있으니 하나씩 만져보면서 놀아보자..

간단한 사용방법은
~~~cs
[SerializeField] ParticleSystem SuccessParticlse;
[SerializeField] ParticleSystem CrashParticles;

이렇게 캐싱해주고

SuccessParticlse.Play();

이렇게 사용해주면 된다.

오브젝트안에 자식으로 넣어주면 간단히 위치를 조정할수 있겠죵?

profile
크래프톤 6기 정글러

0개의 댓글