Photon Player Weapon View Error Help Please
This Character Controller;
public class KarakterKontrol : Photon.MonoBehaviour {
public float hareketHizi;
public float ziplama;
public GameObject kamera;
public GameObject kapsul;
public float kameraAcisi;
void Start () {
}
void Update () {
KarakterHareket();
KameraKontrol();
}
void KameraKontrol()
{
float x = Input.GetAxis("Mouse X");
float y = Input.GetAxis("Mouse Y");
kapsul.transform.Rotate(new Vector3(0, 1, 0) * x);
kameraAcisi -= y;
kameraAcisi = Mathf.Clamp(kameraAcisi, -90, 90);
kamera.transform.localEulerAngles = new Vector3(kameraAcisi, 0, 0);
}
void KarakterHareket()
{
float v = Input.GetAxis("Vertical");
float h = Input.GetAxis("Horizontal");
Vector3 yon = Vector3.Normalize(new Vector3(h, 0, v));
transform.Translate(yon * hareketHizi * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.LeftShift))
{
hareketHizi = 8;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
hareketHizi = 5;
}
RaycastHit ray;
Physics.Raycast(transform.position, Vector3.down, out ray, Mathf.Infinity);
if(ray.distance < 1.2f)
{
if (Input.GetKeyDown(KeyCode.Space))
{
GetComponent<Rigidbody>().AddForce(Vector3.up * ziplama);
}
}
}
}
This Player Network Mover;
public class PlayerNetworkMover : Photon.MonoBehaviour {
public float smoothingDelay = 5;
Vector3 correctPlayersPos = Vector3.zero;
Quaternion correctPlayerRot = Quaternion.identity;
void Start()
{
if (photonView.isMine)
{
GetComponent<KarakterKontrol>().enabled = true;
GetComponentInChildren<Weapon>().enabled = true;
GetComponentInChildren<WeaponSway>().enabled = true;
foreach (Camera cam in GetComponentsInChildren<Camera>())
cam.enabled = true;
transform.Find("MainCamera/GunCamera/Ak-47/ak").gameObject.layer = 9;
}
}
public void Awake()
{
if (this.photonView == null || this.photonView.ObservedComponents != null)
{
Debug.Log(this + "Is not observed");
}
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else
{
correctPlayersPos = (Vector3)stream.ReceiveNext();
correctPlayerRot = (Quaternion)stream.ReceiveNext();
}
}
void Update()
{
if (!photonView.isMine)
{
transform.position = Vector3.Lerp(transform.position, correctPlayersPos, Time.deltaTime * this.smoothingDelay);
transform.rotation = Quaternion.Lerp(transform.rotation, correctPlayerRot, Time.deltaTime * this.smoothingDelay);
}
}
}
And This Network Manager;
public class Connect : Photon.MonoBehaviour {
public static MaxPlayers m_InstanceMax = null;
public static ModParty m_InstanceMod = null;
public GameObject Room;
public Transform[] spawnPoints;
public GameObject myPlayer;
[HideInInspector]
public bool connect = false;
[HideInInspector]
public int ModParty;
private byte Version = 1;
public int PlayerMax = 10;
/// <summary>if we don't want to connect in Start(), we have to "remember" if we called ConnectUsingSettings()</summary>
private bool ConnectInUpdate = true;
private bool Roomcreate = true;
void Awake()
{
DontDestroyOnLoad(gameObject.transform);
}
void Start()
{
PhotonNetwork.autoJoinLobby = false; // we join randomly. always. no need to join a lobby to get the list of rooms.
SceneManager.sceneLoaded += (scene, loadscene) =>
{
if (SceneManager.GetActiveScene().name == "Game")
{
Spawn();
}
};
}
public virtual void Update()
{
if (ConnectInUpdate && connect && !PhotonNetwork.connected)
{
Debug.Log("Update() was called by Unity. Scene is loaded. Let's connect to the Photon Master Server. Calling: PhotonNetwork.ConnectUsingSettings();");
ConnectInUpdate = false;
PhotonNetwork.ConnectUsingSettings(Version + "." + SceneManagerHelper.ActiveSceneBuildIndex);
}
}
public void Spawn()
{
int index = UnityEngine.Random.Range(0, spawnPoints.Length);
myPlayer = PhotonNetwork.Instantiate("Player", spawnPoints[index].position, spawnPoints[index].rotation, 0);
}
// below, we implement some callbacks of PUN
// you can find PUN's callbacks in the class PunBehaviour or in enum PhotonNetworkingMessage
public virtual void OnConnectedToMaster()
{
Debug.Log("Cherche party en mode : "+ModParty);
ExitGames.Client.Photon.Hashtable expectedProperties = new ExitGames.Client.Photon.Hashtable();
expectedProperties.Add( RoomProperty.Type, ModParty.ToString());
PhotonNetwork.JoinRandomRoom( expectedProperties, 0 );
}
public virtual void OnPhotonRandomJoinFailed()
{
Debug.Log("Aucune party, on en crée une.");
RoomOptions roomOptions = new RoomOptions();
roomOptions.maxPlayers = (byte)PlayerMax;
roomOptions.customRoomProperties = new ExitGames.Client.Photon.Hashtable();
roomOptions.customRoomProperties.Add( RoomProperty.Map, "Test" );
roomOptions.customRoomProperties.Add( RoomProperty.Type, ModParty.ToString() );
roomOptions.customRoomPropertiesForLobby = new string[] {
RoomProperty.Map,
RoomProperty.Type,
};
PhotonNetwork.CreateRoom ("TestMap" + "@" + Guid.NewGuid().ToString("N"), roomOptions, null);
Roomcreate = true;
}
public void OnJoinedRoom()
{
Debug.Log("In Room");
AutoFade.m_Instance.BeginFade (-1);
Room.SetActive (true);
Room.GetComponent<ManageListMembers> ().PlayerMax = PlayerMax;
this.gameObject.SetActive (false);
PhotonNetwork.playerName = ClickMenuMain.m_InstancePseudo.pseudo;
m_InstanceMax = (new GameObject("maxplayers")).AddComponent<MaxPlayers>();
m_InstanceMod = (new GameObject("modparty")).AddComponent<ModParty>();
m_InstanceMax.maxplayers = PlayerMax;
m_InstanceMod.modparty = ModParty;
}
}
Hello friends. I am playing a game but when I write the game I wrote the code for the player mover. But when the code runs the code does not work sometimes, but the character moves are shifting, or the gun does not appear, or only one looks. The player's characters seem to be shifting. Friends please help me?
And i got a script from the asset store i bought photon matchmaking v2 i am trying to make a game with it but the photon player network mover is not working please help.
I do not see the gun when I do it. Please help us how to fix it.
Hi,
I don't know if that's the problem, but your CharacterControl script needs to check the photonView.is$$anonymous$$ine condition as well, otherwise the client's input will be processed on each character. $$anonymous$$aybe this is already solved by the Network$$anonymous$$over's Start function. Besides that Network$$anonymous$$over looks okay and should handle the synchronization properly.
Ins$$anonymous$$d of using the Network$$anonymous$$over, you can try using the PhotonTransformView component which is included in the PUN package. Since it basically does the same as your synchronization script, we can check if it is a problem with your code or if it works just fine this way. It also provides some interpolation options for synchronization you can test and see if those are making any difference.
Answer by tatto123321 · Jul 20, 2017 at 12:52 PM
// ----------------------------------------------------------------------------
// // PhotonNetwork Framework for Unity - Copyright (C) 2016 Exit Games GmbH // // // Component to synchronize Transforms via PUN PhotonView. // // developer@exitgames.com // ----------------------------------------------------------------------------
using UnityEngine;
/// /// This class helps you to synchronize position, rotation and scale /// of a GameObject. It also gives you many different options to make /// the synchronized values appear smooth, even when the data is only /// send a couple of times per second. /// Simply add the component to your GameObject and make sure that /// the PhotonTransformView is added to the list of observed components /// [RequireComponent(typeof(PhotonView))] [AddComponentMenu("Photon Networking/Photon Transform View")] public class PhotonTransformView : MonoBehaviour, IPunObservable { //Since this component is very complex, we seperated it into multiple objects. //The PositionModel, RotationModel and ScaleMode store the data you are able to //configure in the inspector while the control objects below are actually moving //the object and calculating all the inter- and extrapolation
[SerializeField]
PhotonTransformViewPositionModel m_PositionModel = new PhotonTransformViewPositionModel();
[SerializeField]
PhotonTransformViewRotationModel m_RotationModel = new PhotonTransformViewRotationModel();
[SerializeField]
PhotonTransformViewScaleModel m_ScaleModel = new PhotonTransformViewScaleModel();
PhotonTransformViewPositionControl m_PositionControl;
PhotonTransformViewRotationControl m_RotationControl;
PhotonTransformViewScaleControl m_ScaleControl;
PhotonView m_PhotonView;
bool m_ReceivedNetworkUpdate = false;
/// <summary>
/// Flag to skip initial data when Object is instantiated and rely on the first deserialized data instead.
/// </summary>
bool m_firstTake = false;
void Awake()
{
this.m_PhotonView = GetComponent<PhotonView>();
this.m_PositionControl = new PhotonTransformViewPositionControl(this.m_PositionModel);
this.m_RotationControl = new PhotonTransformViewRotationControl(this.m_RotationModel);
this.m_ScaleControl = new PhotonTransformViewScaleControl(this.m_ScaleModel);
}
void OnEnable()
{
m_firstTake = true;
}
void Update()
{
if (this.m_PhotonView == null || this.m_PhotonView.isMine == true || PhotonNetwork.connected == false)
{
return;
}
this.UpdatePosition();
this.UpdateRotation();
this.UpdateScale();
}
void UpdatePosition()
{
if (this.m_PositionModel.SynchronizeEnabled == false || this.m_ReceivedNetworkUpdate == false)
{
return;
}
transform.localPosition = this.m_PositionControl.UpdatePosition(transform.localPosition);
}
void UpdateRotation()
{
if (this.m_RotationModel.SynchronizeEnabled == false || this.m_ReceivedNetworkUpdate == false)
{
return;
}
transform.localRotation = this.m_RotationControl.GetRotation(transform.localRotation);
}
void UpdateScale()
{
if (this.m_ScaleModel.SynchronizeEnabled == false || this.m_ReceivedNetworkUpdate == false)
{
return;
}
transform.localScale = this.m_ScaleControl.GetScale(transform.localScale);
}
/// <summary>
/// These values are synchronized to the remote objects if the interpolation mode
/// or the extrapolation mode SynchronizeValues is used. Your movement script should pass on
/// the current speed (in units/second) and turning speed (in angles/second) so the remote
/// object can use them to predict the objects movement.
/// </summary>
/// <param name="speed">The current movement vector of the object in units/second.</param>
/// <param name="turnSpeed">The current turn speed of the object in angles/second.</param>
public void SetSynchronizedValues(Vector3 speed, float turnSpeed)
{
this.m_PositionControl.SetSynchronizedValues(speed, turnSpeed);
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
this.m_PositionControl.OnPhotonSerializeView(transform.localPosition, stream, info);
this.m_RotationControl.OnPhotonSerializeView(transform.localRotation, stream, info);
this.m_ScaleControl.OnPhotonSerializeView(transform.localScale, stream, info);
if (this.m_PhotonView.isMine == false && this.m_PositionModel.DrawErrorGizmo == true)
{
this.DoDrawEstimatedPositionError();
}
if (stream.isReading == true)
{
this.m_ReceivedNetworkUpdate = true;
// force latest data to avoid initial drifts when player is instantiated.
if (m_firstTake)
{
m_firstTake = false;
if (this.m_PositionModel.SynchronizeEnabled)
{
this.transform.localPosition = this.m_PositionControl.GetNetworkPosition();
}
if (this.m_RotationModel.SynchronizeEnabled)
{
this.transform.localRotation = this.m_RotationControl.GetNetworkRotation();
}
if (this.m_ScaleModel.SynchronizeEnabled)
{
this.transform.localScale = this.m_ScaleControl.GetNetworkScale();
}
}
}
}
//void OnDrawGizmos()
//{
// if( Application.isPlaying == false || m_PhotonView == null || m_PhotonView.isMine == true || PhotonNetwork.connected == false )
// {
// return;
// }
// DoDrawNetworkPositionGizmo();
// DoDrawExtrapolatedPositionGizmo();
//}
void DoDrawEstimatedPositionError()
{
Vector3 targetPosition = this.m_PositionControl.GetNetworkPosition();
// we are synchronizing the localPosition, so we need to add the parent position for a proper positioning.
if (transform.parent != null)
{
targetPosition = transform.parent.position + targetPosition ;
}
Debug.DrawLine(targetPosition, transform.position, Color.red, 2f);
Debug.DrawLine(transform.position, transform.position + Vector3.up, Color.green, 2f);
Debug.DrawLine(targetPosition , targetPosition + Vector3.up, Color.red, 2f);
}
//void DoDrawNetworkPositionGizmo()
//{
// if( m_PositionModel.DrawNetworkGizmo == false || m_PositionControl == null )
// {
// return;
// }
// ExitGames.Client.GUI.GizmoTypeDrawer.Draw( m_PositionControl.GetNetworkPosition(),
// m_PositionModel.NetworkGizmoType,
// m_PositionModel.NetworkGizmoColor,
// m_PositionModel.NetworkGizmoSize );
//}
//void DoDrawExtrapolatedPositionGizmo()
//{
// if( m_PositionModel.DrawExtrapolatedGizmo == false ||
// m_PositionModel.ExtrapolateOption == PhotonTransformViewPositionModel.ExtrapolateOptions.Disabled ||
// m_PositionControl == null )
// {
// return;
// }
// ExitGames.Client.GUI.GizmoTypeDrawer.Draw( m_PositionControl.GetNetworkPosition() + m_PositionControl.GetExtrapolatedPositionOffset(),
// m_PositionModel.ExtrapolatedGizmoType,
// m_PositionModel.ExtrapolatedGizmoColor,
// m_PositionModel.ExtrapolatedGizmoSize );
//}
}