본문 바로가기

유니티(Unity)

유니티(Unity) 플레이어 벽 뚫림 방지

해당 스크립트는 벽 오브젝트에 넣는다. 일반적으로 플레이어 속도가 빠를 때 뚫고 지나가버리는 현상이 나타나므로

벽에 부딪쳤을 때, 플레이어 속도를 제어하는 방식으로 뚫림현상을 막는다.

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

public class PlayerStop_Wall : MonoBehaviour
{
    private int trigger = 0;
    void OnCollisionEnter(Collision other)
    {
        Debug.Log("콜리전enter 작동중");
        if(trigger == 0)
        {
            if (other.collider.CompareTag("Player"))
            {
                other.transform.parent.GetComponent<PlayerMoveAndCamera>().speedSetting = 1f;
                trigger = 1;
            }
        }
    }
    void OnCollisionExit(Collision other)
    {
        Debug.Log("콜리전exit 작동중");
        if (trigger == 1)
        {
            if (other.collider.CompareTag("Player"))
            {
                other.transform.parent.GetComponent<PlayerMoveAndCamera>().speedSetting = 10f;
                trigger = 0;
            }
        }
    }
}

'유니티(Unity)' 카테고리의 다른 글

Unity 디자인 패턴  (0) 2023.02.05
유니티(Unity) Netcode Relay 주의할 점  (0) 2023.02.04
Unity Netcode 절차  (0) 2022.11.19
Unity 동기 비동기 async await  (0) 2022.10.26
오브젝트 정보 UI 에 표시하기  (0) 2022.10.25