[SOLVED] Problem adding forces via RPC

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:
// 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

Comments

  • JohnTubeJohnTube mod
    edited September 12
    Hi @Luixma,

    Make sure FixedUpdate is actually called.
    Make sure GameManager.GAME_RUNNING is true.
    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 of

    collision.gameObject.tag == "Player"

    do

    collision.gameObject.CompareTag("Player")

    2. Make use of PhotonView.owner:

    instead of

    PhotonPlayer.Find(collision.gameObject.GetComponent<PhotonView>().viewID)

    do

    collision.gameObject.GetComponent<PhotonView>().owner
  • 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:
    Anotaci-n-2019-09-12-174422

    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?
  • Hi @Luixma;

    You need to change how you get the target Player from Collision.

    this is clearly wrong as it gets the local player's PhotonView.

    collision.gameObject.GetComponent<PhotonView>()
  • LuixmaLuixma
    edited September 13
    Thanks for your help @JohnTube

    i think i am not getting the PhotonTarget of the local player. Becouse i am using
    private void OnCollisionEnter(Collision collision)
    {
    if (collision.gameObject.CompareTag("Player") && playerOnline.playerCanMove
    && transform.position.x < collision.transform.position.x)
    {

    ......
    playerOnline.PushPlayer(collision.gameObject.GetComponent<PhotonView>().owner, direction); } }
    i am getting the PhotonView of the remote player with
    playerOnline.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..
  • The question is..
    How can I send a rpc only to the player collided with the local player?
    How should be the code?

    Thank you
  • LuixmaLuixma
    edited September 14
    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?
  • @Luixma

    I'm glad you managed to solve this.
    Thanks for the update.

    I've edited the discussion title.
Sign In or Register to comment.