using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class SingleShotGun : Gun
//Gun으로부터 정보받기
{
[SerializeField] Camera cam;
PhotonView PV;
void Awake()
{
PV = GetComponent<PhotonView>();
}
public override void Use()
{
Debug.Log("using gun " + itemInfo.itemName);
Shoot();
}
void Shoot()
{
Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f));
//카메라로부터 레이저 발사
ray.origin = cam.transform.position;
//발사지점 카메라로부터
if (Physics.Raycast(ray, out RaycastHit hit))
{
hit.collider.gameObject.GetComponent<IDamageable>()?.TakeDamage(((GunInfo)itemInfo).damage);
//레이저에 맞은 물체가 데미지를 입을 수 있는 물체인가? 맞다면 아이템 정보에 맞는 데미지 받기
PV.RPC("RPC_Shoot", RpcTarget.All, hit.point,hit.normal );
//맞추면 상대방한테 맞았다고 알려주기
}
}
[PunRPC]//Pun Remote Procedure Call의 약자로써 원격제어를 통해 함수를 실행시키는 기능이다.
void RPC_Shoot(Vector3 hitPosition,Vector3 hitNoraml)
{//hitNormal은 맞는 면이 향하는 방향이다.
Collider[] colliders = Physics.OverlapSphere(hitPosition, 0.3f);
//0.3f 반경내에 collider들을 검출한다.
if (colliders.Length != 0)//콜라이더가 존재한다면
{
GameObject bulletImpactObj=Instantiate(bulletImpactPrefab, hitPosition + hitNoraml * 0.001f, Quaternion.LookRotation(hitNoraml, Vector3.up) * bulletImpactPrefab.transform.rotation);
//hitPosition+hitNormal*0.001f를 해주면 총알 자국이 대상표면 보다 아주 살짝 위에있게되서 겹쳐보이지 않는다.
Destroy(bulletImpactObj, 10f);
//총알 자국은 10초후에는 사라진다.
bulletImpactObj.transform.SetParent(colliders[0].transform);
//주변에 있는 콜라이더를 부모로 설정한다.
//부모가 사라지면 자식물체들도 다 사라지므로 총알자국만 둥둥떠다니는걸 해결 할 수 있다.
}
}
}