유니티 3D 멀티플레이 RPC이용법(플레이어 체력 깎기)
당연한 얘기지만 총에 맞으면 체력이 달아야 한다.
일단 위와 같이 적당히 체력바를 만들어 준다.
MyPlayer.cs에
위 두 변수를 만들어 준다.
playerHealth는 말그대로 플레이어의 체력이고 damage는 총알 한개의 데미지이다.
이런 변수 수치는 개발자 맘대로 하자.
같은 스크립트의 Fire()함수를 아래와 같이 수정한다
if(Physics.Raycast(rayOrigin.position,Camera.main.transform.forward,out hit,100f))
{
Debug.Log(hit.transform.tag);
if(hit.transform.tag=="Player"&&!hit.transform.GetComponent<PhotonView>().IsMine)//Not hitting myself
{
hit.transform.GetComponent<PhotonView>().RPC("GetDamage",RpcTarget.AllBuffered,damage);
}
}//플레이어 위치에서 카메라의 방향으로 100f거리만큼 Ray를 쏜다
카메라 정중앙으로 Ray를 쏴서 플레이어만 맞게, 또한 나 자신은 맞지않게
!hit.transform.GetComponent<PhotonView>().IsMine 을 넣어준다.
만약 상대플레이어한테 맞았다면
hit.transform.GetComponent<PhotonView>().RPC("GetDamage",RpcTarget.AllBuffered,damage);
를 실행한다.
RPC(Remote procedure call)
원격 프로시저 호출이라고도 하며 별도의 원격 제어를 위한 코딩 없이 다른 주소 공간에서 함수나 프로시저를
실행할 수 있게하는 프로세스 간 통신 기술이다. 게임에 적용해서 말하자면 같은 룸에 있는 다른 유저(클라이언트)의 함수를 실행하는 것이다.
한마디로 여기서는 Ray를 맞은 플레이어는 RPC에 의해 "GetDamage" 라는 함수를 실행한다.
여기서 또 MyPlayer.cs에 GetDamage라는 함수를 만들어준다
저 파라미터 (float amount)는
에서 마지막 damage이다.
RpcTarget에는 많은 옵션이 있으니 참고삼아 이미지 첨부한다.
실행영상
위 영상에서 상대 플레이어가 총을 쏠때마다 오른쪽 인스펙터창의 myPlayer에서 PlayerHealth가 미리 설정해둔
damage 0.01씩 깍이는 걸 볼 수 있다. 물론 총을 연사하면 데미지가 쭉쭉 달아야 하는게 정상이지만 지금은 일단
총에맞으면 체력이 깍이는 것을 보여주기 위해 최대한 간단하게 구현하였다.
어쨋든 이런식으로 응용하면 피격효과도 낼 수있고 피격사운드도 낼 수 있다.
출처:
https://doc-api.photonengine.com/en/pun/v2/group__public_api.html
Photon Unity Networking 2: Public API
Groups the most important classes that you need to understand early on. More... enum ClientState State values for a client, which handles switching Photon server types, some operations, etc. More... enum PunLogLevel Used to define the level of l
doc-api.photonengine.com