Hey guys,
i've got an issue with a rpc call. I want to lower the health of another player when he gets hit by it the gameobject has this script attached.
The issue is that the gameobjects collides two or three times with the other player and so there player gets to much damage. There should just be one collision and the object should get destroyed.
using UnityEngine;
using System.Collections;
public class SkillScript : Photon.MonoBehaviour
{
public int skilldmg;
private GameObject target;
void Start()
{
skilldmg = 60;
}
void OnCollisionEnter(Collision collision)
{
if (collision.collider.tag == "PlayerHead") // Enemys networked head
{
target = collision.collider.gameObject;
PhotonView photonView = PhotonView.Get(this);
photonView.RPC("LowerHp", PhotonTargets.All); // Call to RPC
PhotonNetwork.Destroy(gameObject); // Destroy this GO
}
}
[PunRPC]
public void LowerHp()
{
target.GetComponent().curHp -= skilldmg; // Enemy get damage
if (target.GetComponent().curHp <= skilldmg)
{
if (photonView.isMine)
{
GameObject.FindGameObjectWithTag("Player").GetComponent<LocalScript>().myHead.GetComponent().gold += 50; // Player get gold
}
}
}
}
Here is the Script to sync transform:
using UnityEngine;
using System.Collections;
public class NetMovement : Photon.MonoBehaviour {
private Vector3 correctPos;
private Quaternion correctRot;
void Start()
{
PhotonView photonView = GetComponent();
}
void Update()
{
if (!photonView.isMine)
{
transform.position = Vector3.Lerp(transform.position, this.correctPos, Time.deltaTime * 100); // Maybe to much?
transform.rotation = Quaternion.Lerp(transform.rotation, this.correctRot, Time.deltaTime * 5);
}
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else
{
this.correctPos = (Vector3)stream.ReceiveNext();
this.correctRot = (Quaternion)stream.ReceiveNext();
}
}
}
Thank you guys!
i've got an issue with a rpc call. I want to lower the health of another player when he gets hit by it the gameobject has this script attached.
The issue is that the gameobjects collides two or three times with the other player and so there player gets to much damage. There should just be one collision and the object should get destroyed.
using UnityEngine;
using System.Collections;
public class SkillScript : Photon.MonoBehaviour
{
public int skilldmg;
private GameObject target;
void Start()
{
skilldmg = 60;
}
void OnCollisionEnter(Collision collision)
{
if (collision.collider.tag == "PlayerHead") // Enemys networked head
{
target = collision.collider.gameObject;
PhotonView photonView = PhotonView.Get(this);
photonView.RPC("LowerHp", PhotonTargets.All); // Call to RPC
PhotonNetwork.Destroy(gameObject); // Destroy this GO
}
}
[PunRPC]
public void LowerHp()
{
target.GetComponent().curHp -= skilldmg; // Enemy get damage
if (target.GetComponent().curHp <= skilldmg)
{
if (photonView.isMine)
{
GameObject.FindGameObjectWithTag("Player").GetComponent<LocalScript>().myHead.GetComponent().gold += 50; // Player get gold
}
}
}
}
Here is the Script to sync transform:
using UnityEngine;
using System.Collections;
public class NetMovement : Photon.MonoBehaviour {
private Vector3 correctPos;
private Quaternion correctRot;
void Start()
{
PhotonView photonView = GetComponent();
}
void Update()
{
if (!photonView.isMine)
{
transform.position = Vector3.Lerp(transform.position, this.correctPos, Time.deltaTime * 100); // Maybe to much?
transform.rotation = Quaternion.Lerp(transform.rotation, this.correctRot, Time.deltaTime * 5);
}
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else
{
this.correctPos = (Vector3)stream.ReceiveNext();
this.correctRot = (Quaternion)stream.ReceiveNext();
}
}
}
Thank you guys!