Quantcast
Channel: Recent Discussions — Photon Engine
Viewing all articles
Browse latest Browse all 15755

Multiplayer screen wrap troubles

$
0
0
Hey everyone! I am very new to photon and I am trying to get it to work on a Joust like game. Right now I am having problems with the screen wrap. What is happening is that when the player goes to one side of the screen instead of them just popping on the other it will kinda rubberband them to the other side. If needed I can show a video. Here are my scripts though.

Thanks in advance for the help!

This is the loop script that handles the screen warp:
using UnityEngine;
using System.Collections;

public class loop : MonoBehaviour {

private Vector3 horizontalOffset;
private float horizontalEdge;

void Start () {
//figure out the screen bounds
horizontalOffset = Camera.main.ViewportToWorldPoint (new Vector3 (1.5f, 0.5f, -Camera.main.transform.position.z));
horizontalEdge = Camera.main.ViewportToWorldPoint (new Vector3 (1.0f, 0.5f, -Camera.main.transform.position.z)).x;
}

// Update is called once per frame
void Update () {
//Check to see if the object has exceeded the screen bounds
if (transform.position.x > horizontalEdge) {
transform.position -= horizontalOffset;
}
else if (transform.position.x < -horizontalEdge) {
transform.position += horizontalOffset;
}
}


}


And this is the networkplayer script:
using UnityEngine;
using System.Collections;

public class NetworkPlayer1 : Photon.MonoBehaviour {
//
// A (very) simple network interpolation script, using Lerp().
//
// This will lag-behind, compared to the moving cube on the controlling client.
// Actually, we deliberately lag behing a bit more, to avoid stops, if updates arrive late.
//
// This script does not hide loss very well and might stop the local cube.
//
private Vector3 latestCorrectPos;
private Vector3 onUpdatePos;
private float fraction;


public void Start()
{
this.latestCorrectPos = transform.position;
this.onUpdatePos = transform.position;
}


public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
Vector3 pos = transform.localPosition;
Quaternion rot = transform.localRotation;
Vector3 scal = transform.localScale;
stream.Serialize(ref pos);
stream.Serialize(ref rot);
stream.Serialize(ref scal);
}
else
{
// Receive latest state information
Vector3 pos = Vector3.zero;
Quaternion rot = Quaternion.identity;
Vector3 scal = Vector3.zero;


stream.Serialize(ref pos);
stream.Serialize(ref rot);
stream.Serialize(ref scal);


this.latestCorrectPos = pos; // save this to move towards it in FixedUpdate()
this.onUpdatePos = transform.localPosition; // we interpolate from here to latestCorrectPos
this.fraction = 0; // reset the fraction we alreay moved. see Update()

transform.localRotation = rot; // this sample doesn't smooth rotation
transform.localScale = scal;
}
}


public void Update()
{
if (this.photonView.isMine)
{
return; // if this object is under our control, we don't need to apply received position-updates
}

// We get 10 updates per sec. Sometimes a few less or one or two more, depending on variation of lag.
// Due to that we want to reach the correct position in a little over 100ms. We get a new update then.
// This way, we can usually avoid a stop of our interpolated cube movement.
//
// Lerp() gets a fraction value between 0 and 1. This is how far we went from A to B.
//
// So in 100 ms, we want to move from our previous position to the latest known.
// Our fraction variable should reach 1 in 100ms, so we should multiply deltaTime by 10.
// We want it to take a bit longer, so we multiply with 9 instead!

this.fraction = this.fraction + Time.deltaTime * 9;
transform.localPosition = Vector3.Lerp(this.onUpdatePos, this.latestCorrectPos, this.fraction); // set our pos between A and B
}
}

Viewing all articles
Browse latest Browse all 15755

Trending Articles