[SOLVED] Problem adding forces via RPC
Luixma
✭
Hi!
i have been searching about this topic but i cant found anything.
I have a problem when my player collide with other. First the player that is behind the other its pushed locally backward, and his positions its synchronized (all perfect), but this player send a RPC too to the player that is i front of him to be pushed frontward. And its not pushed anyway.
here its my code:
I hope you can found a solution and thanks for reading
i have been searching about this topic but i cant found anything.
I have a problem when my player collide with other. First the player that is behind the other its pushed locally backward, and his positions its synchronized (all perfect), but this player send a RPC too to the player that is i front of him to be pushed frontward. And its not pushed anyway.
here its my code:
// Push the local player if he is in a before position than the other player collided private void OnCollisionEnter(Collision collision) { if (collision.gameObject.tag == "Player" && playerOnline.playerCanMove && transform.position.x < collision.transform.position.x) { // minus because we want push this player in oposite direction Vector3 direction = (collision.transform.position - transform.position).normalized; playerOnline.factorPosition *= 0.75f; playerOnline.maxVelocity *= 0.75f; rigidBody.AddForce(-direction * playerOnline.forcePushPlayers, ForceMode.Impulse); // push the other player playerOnline.PushPlayer(PhotonPlayer.Find(collision.gameObject.GetComponent<PhotonView>().viewID), direction); } }
// Called by PlayerCollisions internal void PushPlayer(PhotonPlayer target, Vector3 direction) { photonView.RPC("PushedByOtherPlayer", target, direction, true); } [PunRPC] internal void PushedByOtherPlayer(Vector3 direction, bool newPushed, PhotonMessageInfo info) { Debug.Log(info); Debug.Log("newPushed: " + newPushed); pushed = newPushed; Debug.Log("Pushed: " + this.pushed); Debug.Log("player: " + PhotonNetwork.playerName + ", id: " + photonView.viewID); directionPush = direction; }i can see that the pushed variable its ok in the log (true). (I have tried to add force to the player in this method too and not use a bool, but not works)
// for local player physics private void FixedUpdate() { if (GameManager.GAME_RUNNING && photonView.isMine) { .......... if (pushed) { Debug.Log("PUSHED!!!!!!!!!!!!!!!!!!"); factorPosition *= 1.25f; maxVelocity *= 1.25f; rigidBody.AddForce(directionPush * forcePushPlayers, ForceMode.Impulse); pushed = !pushed; } } }i cant see "PUSHED!!!!!!!!!!!!!!!!!!"never!!
I hope you can found a solution and thanks for reading
0
Comments
-
Hi @Luixma,
Make sureFixedUpdate
is actually called.
Make sureGameManager.GAME_RUNNING
istrue
.
So I would add:private void FixedUpdate() { if (GameManager.GAME_RUNNING && photonView.isMine) { Debug.LogFormat("pushed={0}", pushed);
If you don't see, this then add:private void FixedUpdate() { Debug.LogFormat("GameManager.GAME_RUNNING={0} photonView.isMine={1}", GameManager.GAME_RUNNING, photonView.isMine); if (GameManager.GAME_RUNNING && photonView.isMine) {
Otherwise, I wanted to give you two tips unrelated to your question:
1. Use CompareTag as it's better performance-wise:
instead ofcollision.gameObject.tag == "Player"
docollision.gameObject.CompareTag("Player")
2. Make use ofPhotonView.owner
:
instead ofPhotonPlayer.Find(collision.gameObject.GetComponent<PhotonView>().viewID)
docollision.gameObject.GetComponent<PhotonView>().owner
0 -
thanks @JohnTube , i have put this log
Debug.LogFormat("GameManager.GAME_RUNNING={0} photonView.isMine={1} pushed={2}", GameManager.GAME_RUNNING, photonView.isMine, pushed);
and i see this:
the rpc arrive to player who sent the RPC, not the target. Why can be this possible if I select as target the local player in the another client?0 -
Thanks for your help @JohnTube
i think i am not getting the PhotonTarget of the local player. Becouse i am usingprivate void OnCollisionEnter(Collision collision) { if (collision.gameObject.CompareTag("Player") && playerOnline.playerCanMove && transform.position.x < collision.transform.position.x) { ...... <b class="Bold"> playerOnline.PushPlayer(collision.gameObject.GetComponent<PhotonView>().owner, direction);</b> } }
i am getting the PhotonView of the remote player withplayerOnline.PushPlayer(collision.gameObject.GetComponent<PhotonView>().owner, direction);
(not local, becouse the local player are hiring the OnCollisionEnter event becouse his position.x is less than the remote player)
LocalPlayer (x=0, y=0, z=0) (Collision) RemotePlayer (x=1, y=0, z=0)
LocalPlayer receive OnCollisionEvent and send RPC to collision.gameObject (should be the remote player right?)
maybe i am incorrect, or i have to get photonTarget with other way... i am trouble with this some days ago..0 -
The question is..
How can I send a rpc only to the player collided with the local player?
How should be the code?
Thank you0 -
Solution:
change:
playerOnline.PushPlayer(collision.gameObject.GetComponent().owner, direction);
to:
other.gameObject.GetComponent().PushPlayer(other.gameObject.GetComponent().owner, direction);
I have to use the photonview of the gameobject that i want to push forward, not the local player
thanks for reading and the solution is the answer of this topic:
https://forum.photonengine.com/discussion/13713/need-to-know-the-bullet-is-coming-from-which-playe#latest
@JohnTube solved! i cant edit the title of the topic to add [SOLVED], its possible?
0