Call another instance/player's method
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
Call another instance/player's method
Vukky
2020-03-02 02:02:55
Hello guys, this will be a noob question, sorry about that.
So, i'm actually making a car arena/derby game, the cars/players should damage each other on collision, based on velocity. When a collision happens i call the colliding objects method and damage it with my velocity, the problem that this only affect the other player's HP locally, but from it's view we don't change it's HP, is there a proper way to do it?
My code:
private void OnCollisionEnter(Collision theCollision)
{
if (!photonView.IsMine)
{
return;
}
if (theCollision.gameObject.tag == "Car")
{
var collisionVelocity = theCollision.relativeVelocity.magnitude - this.rigidBody.velocity.magnitude;
theCollision.collider.GetComponent<carPhysics>().DamageCar(collisionVelocity);
}
}
public void DamageCar(float damageMultiplier)
{
float baseDamage = 1.0f;
float damage = baseDamage + damageMultiplier;
this.health -= damage;
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
stream.SendNext(health);
}
else
{
this.health = (float)stream.ReceiveNext();
}
}
Where carPhysics is the name of the script which i attached to the Car prefab, and when 2 Car collides.
Comments
cranemountaingaming
2020-03-23 01:33:34
Make sure your class that contains OnPhotonSerializeview inherits from IPunObservable like so:
public class YourClass : MonoBehaviourPunCallbacks, IPunObservable
And make your class observable by adding the following in Start():
if (!photonView.ObservedComponents.Contains(this))
{
photonView.ObservedComponents.Add(this);
}
I'm a novice, so I might have misunderstood your question. Or it's possible you have already done all this and just didn't show those segments. But, it looks to me like you're doing it correctly, so I thought maybe it's the setup and not the execution that is not quite correct.
Thank you for your reply!
I figured out the solution, so i share it:
If you would like to do something with another player's object (in my case damage the other player's car), you should call an RPC, and say that object that hey, you need to decrease your health because I hit you!
The RPC( Remote Procedure Call) spreads over the network, and if someone's PhotonView ViewID equal with the searched PhotonViewID,which i pass as parameter, it decreases it's heath.
My Script:
public class playerCar : MonoBehaviourPunCallbacks, IPunObservable
{
public float health = 100f;
PhotonView thisPhotonView;
public void Start()
{
thisPhotonView = this.gameObject.GetComponent<PhotonView>();
}
private void OnCollisionEnter(Collision theCollision)
{
if (!photonView.IsMine)
{
return;
}
if (theCollision.gameObject.tag == "Car")
{
var collisionVelocity = theCollision.relativeVelocity.magnitude - this.rigidBody.velocity.magnitude;
// ask the players car Phototn View ID, which we collide, and damage it trough RPC call
int Collidedviewid = theCollision.collider.GetComponent<PhotonView>().ViewID;
theCollision.collider.GetComponent<playerCar>().photonView.RPC("DamageCar", RpcTarget.All, collisionVelocity, Collidedviewid);
}
[PunRPC]
public void DamageCar(float damageMultiplier, int viewID)
{
// Is Someone collided with our car? then damage it
if (thisPhotonView.ViewID == viewID)
{
float baseDamage = 1.0f;
float damage = baseDamage + damageMultiplier;
this.health -= damage;
}
}
}
This code maybe looks dirty, but it works, if you know how to do it better, please don't keep it for yourself!
Ps : Be aware, that this script works as a client side verfied thing, which is don't the best thing, due to cheating may happen.