I'm making a 2D shooter game, and I'm running into some errors.
Received RPC "DestroyRPC" for viewID 2028 but this PhotonView does not exist! Was remote PV. Owner called. By: #02 'davdv' Maybe GO was destroyed but RPC not cleaned up.
Received OnSerialization for view ID 2004. We have no such PhotonView! Ignore this if you're joining or leaving a room. State: Joined
These two errors continue to occur while the bullet is firing.
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviourPunCallbacks
{
private Vector3 dir;
public Camera cam;
public GameStart GS;
public float bulletSpeed = 10f; // 총알의 속도
public GameObject bulletPrefab; // 총알의 프리팹
// Start is called before the first frame update
private void Awake()
{
GS = FindObjectOfType<GameStart>();
}
void Start()
{
cam = Camera.main;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0)&&photonView.IsMine)
{
if (GS.Isshot)
{
dir = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -cam.transform.position.z));
photonView.RPC("ShootBullet", RpcTarget.AllBuffered, dir);
}
}
}
[PunRPC]
void ShootBullet(Vector3 dir)
{
if (photonView.IsMine)
{
// 총알 생성 위치 계산
Vector3 bulletSpawnPosition = transform.position;
GameObject bullet = PhotonNetwork.Instantiate("Bullet", bulletSpawnPosition, Quaternion.identity);
bullet.GetComponent<Rigidbody2D>().velocity = (dir - transform.position).normalized * bulletSpeed;
}
}
}
[weapon.cs]
using Photon.Pun;
using Photon.Realtime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviourPunCallbacks
{
public float bulletDamage;
public PhotonView PV;
void Start() => Destroy(gameObject, 3.5f);
void OnTriggerEnter2D(Collider2D hitInfo)
{
if(hitInfo.tag=="Border") this.PV.RPC("DestroyRPC", RpcTarget.AllBuffered);
PhotonView target = hitInfo.gameObject.GetComponent<PhotonView>();
if (target != null && (!target.IsMine))
{
if (target.tag == "Player"&&!PV.IsMine&&hitInfo.GetComponent<PhotonView>().IsMine)
{
target.RPC("reduceHealth", RpcTarget.All, bulletDamage);
this.PV.RPC("DestroyRPC", RpcTarget.AllBuffered);
}
}
}
[PunRPC]
void DestroyRPC()
{
Destroy(this.gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.UI;
using TMPro;
using System.Xml.Serialization;
using Cinemachine;
using System;
using System.Runtime.CompilerServices;
public class Player : MonoBehaviourPunCallbacks, IPunObservable
{
public GameObject CamRange;
public Transform diepoint;
public float moveSpeed = 5f; // 플레이어의 이동 속도
public TextMeshProUGUI NickNameText;
public TextMeshProUGUI KillText;
public int killNum;
public int randrespawn;
public bool immortal;
public GameStart GS;
private Rigidbody2D RB;
private PhotonView PV;
private Quaternion networkRotation;
Vector3 curPos;
public Camera cam;
void Awake()
{
CamRange = GameObject.Find("CAMRANGE");
GS=FindObjectOfType<GameStart>();
immortal = false;
RB = GetComponent<Rigidbody2D>();
PV = GetComponent<PhotonView>();
NickNameText.text = PV.IsMine ? PhotonNetwork.NickName : PV.Owner.NickName;
NickNameText.color = PV.IsMine ? Color.green : Color.red;
}
void Start()
{
diepoint = GameObject.Find("diepoint").transform;
cam = Camera.main;
}
public void TeleportToTarget(Vector3 targetPosition)
{
transform.position = targetPosition;
}
private void Update()
{
KillText.text = $"{killNum}kill";
if (photonView.IsMine)
{
float axisx = Input.GetAxisRaw("Horizontal");
float axisy = Input.GetAxisRaw("Vertical");
RB.velocity = new Vector2(4 * axisx, 4 * axisy);
var CM = GameObject.Find("CMCamera").GetComponent<CinemachineVirtualCamera>();
CM.Follow = transform;
CM.LookAt = transform;
// 마우스 클릭 시 발사 처리
}
else if ((transform.position - curPos).sqrMagnitude >= 100) transform.position = curPos;
else transform.position = Vector3.Lerp(transform.position, curPos, Time.deltaTime * 10);
}
public void Hit()
{
if (!immortal)
{
CamRange.SetActive(false);
immortal = true;
transform.Translate(diepoint.position);
StartCoroutine(WaitCoroutine());
}
}
[PunRPC]
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
// 자신의 위치 및 회전 정보를 네트워크로 전송
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else
{
// 네트워크로부터 위치 및 회전 정보를 수신
curPos = (Vector3)stream.ReceiveNext();
networkRotation = (Quaternion)stream.ReceiveNext();
}
}
IEnumerator WaitCoroutine()
{
yield return new WaitForSeconds(5.0f);
CamRange.SetActive(true);
transform.position = Vector3.zero;
Invoke("Immortal", 2.0f);
}
private void Immortal()
{
immortal = false;
}
}
bullet.cs->bullet
player.cs /weapon.cs->player
I modified the code to solve the error and looked at the related code.