Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

Multiplayer photon game help

A topic by BongDev created May 26, 2021 Views: 722 Replies: 6
Viewing posts 1 to 6

please help me how to sync line renderer this is my code:-


using UnityEngine;

using Photon.Pun;

public class Grappling : MonoBehaviourPunCallbacks

{

    private LineRenderer lr;

    private Vector3 grapplePoint;

    public LayerMask whatIsGrappleable;

    public Transform gunTip, player;

    public Camera camera;

    private float maxDistance = 100f;

    private SpringJoint joint;

    PhotonView PV;

    

    void Awake()

    {

        lr = GetComponent<LineRenderer>();

        PV = GetComponent<PhotonView>();

    }

    void Update()

    {

        if (PV.IsMine)

        {

            if (Input.GetMouseButtonDown(1))

            {

                StartGrapple();

            }

            else if (Input.GetMouseButtonUp(1))

            {

                StopGrapple();

            }

        }

    }

    //Called after Update

    void LateUpdate()

    {

        DrawRope();

    }

    /// <summary>

    /// Call whenever we want to start a grapple

    /// </summary>

    

    void StartGrapple()

    {

        RaycastHit hit;

        if (Physics.Raycast(camera.transform.position, camera.transform.forward, out hit, maxDistance, whatIsGrappleable))

        {

            grapplePoint = hit.point;

            joint = player.gameObject.AddComponent<SpringJoint>();

            joint.autoConfigureConnectedAnchor = false;

            joint.connectedAnchor = grapplePoint;

            float distanceFromPoint = Vector3.Distance(player.position, grapplePoint);

            //The distance grapple will try to keep from grapple point. 

            joint.maxDistance = distanceFromPoint * 0.8f;

            joint.minDistance = distanceFromPoint * 0.25f;

            //Adjust these values to fit your game.

            joint.spring = 4.5f;

            joint.damper = 7f;

            joint.massScale = 4.5f;

            lr.positionCount = 2;

            currentGrapplePosition = gunTip.position;

        }

    }

    /// <summary>

    /// Call whenever we want to stop a grapple

    /// </summary>

    

    void StopGrapple()

    {

        lr.positionCount = 0;

        Destroy(joint);

    }

    private Vector3 currentGrapplePosition;

    

    void DrawRope()

    {

        

            PV.RPC("RPC_DrawRope", RpcTarget.AllBuffered);

    }

    public bool IsGrappling()

    {

        return joint != null;

    }

    public Vector3 GetGrapplePoint()

    {

        return grapplePoint;

    }

    [PunRPC]

    void RPC_DrawRope()

    {

        //If not grappling, dont' draw rope

        if (!joint) return;

        currentGrapplePosition = Vector3.Lerp(currentGrapplePosition, grapplePoint, Time.deltaTime * 8f);

        lr.SetPosition(0, gunTip.position);

        lr.SetPosition(1, currentGrapplePosition);

    }

}

Moderator moved this topic to General Development
(2 edits)

Hello Ariyo,

I'd like to help you if it's possible. 

Can you explain something more about what kind of problem are you having?

Have you tried to communicate with the Photon addon developers first?

Then, while not being able to run your code, I've read it and I've noticed a few things that you may check.


1. StartGrapple(). Adding a new component not only with every click but continuously.

Check this line:

 joint = player.gameObject.AddComponent<SpringJoint>();

You're calling StartGrapple in every frame while your mouse button is down. 

I suspect that you're not controlling that your player will have only one joint component active at the moment and that you're creating and adding multiple SpringJoint components to your player. Non stop. Possibly, you may need to control that and allow only one joint at one moment.


2. StopGrapple(). Destroying a joint without disconnecting,

Here I have not so much experience and I may be wrong, but I think that the connected objects to the joint may be looked after before destroying the link/joint. ("looked after before destroying", english is so funny sometimes...) Not sure here, but destroying the joint without looking may give wonky results in the physics system.

What about just disconnecting and reconnecting instead of destroying and creating SpringJoint components? Maybe that could help.


3. Try to keep your code clean the best you can.

I know it's only one, but you left this little guy...

    private Vector3 currentGrapplePosition;

... lonely and abandoned between methods, being bullied down the alley, while you have all your other variables on the top of the page, what is a good thing, but not for the little guy.

Sorry for not being able to help you more at the moment.

Good luck!

Hey synced the line renderer but giving error line renderer out of bounds when processing it in Rpc_DrawRope method

Hi,

I've noticed that you define how many points the LineRenderer will use when you click (inside StartGrapple())

 lr.positionCount = 2;

But...

I'm not sure if this

if (!joint) return;

is really stopping the method if the joint is not existing yet. 

I prefer to use something like:

if (joint == null) return;

Or at least, I'm more used to that expression to control if something exists.

Then, if I'm correct, it's possible that the LineRenderer is trying to draw points before the number of points has been defined (RPC_DrawRope is being called every frame in LateUpdate) 

I mean, maybe I'm wrong, but it's possible that you are asking to draw the LineRenderer second position and it's possible that LineRenderer still doesn't know how many points it has to draw.

What about trying to setup the LineRenderer in the Awake method? Awake is launched before Updates. 

Again, trying to help here, but I'm unable to run your code and I'm debugging just with my eyes :(

Good luck!

Can't figure it out!! 😰😰😰

(2 edits)

I can't run your code right now and I might be missing something, but it seems that you're not transmitting grapplePoint -variable to other clients.

You pass it as a parameter to RPC_DrawRope (see https://doc.photonengine.com/en-us/pun/current/gameplay/rpcsandraiseevent):

RPC call:

PV.RPC("RPC_DrawRope", RpcTarget.All, grapplePoint);

RPC definition:

[PunRPC]
void RPC_DrawRope(Vector3 grapplePoint) { 
    this.grapplePoint = grapplePoint; 
    ...
}

There are better ways, though. Consider using OnPhotonSerializeView -method for synchronizing variables (https://doc.photonengine.com/en-us/pun/current/demos-and-tutorials/pun-basics-tutorial/player-networking).

I have figured it out thanks for help

Deleted 1 year ago