[SOLVED] HELP!! PUN is not working properly!

Options
Hi, first of all.
I'm developing a 2D game using PUN to use real time variables, like the player's position.
When i make 2 instances of the game (Unity Web Player and Unity Editor) i see the two player's in the first instance i created, but in the last i dont see the two player's, only see my player, any help?
Thanks
Dinis Gomes
using UnityEngine;
using System.Collections;

public class Player : Entity {

	public ItemPlayerManager ipManager;

	public SpriteRenderer weaponSpriteParent;
	public Vector2 forwardPos, backPos, sidePos;

	public WeaponManager weaponManager;

	public int money = 500;
	public bool ismine = true;

	private float lastSynchronizationTime = 0f;
	private float syncDelay = 0f;
	private float syncTime = 0f;
	private Vector3 syncStartPosition = Vector3.zero;
	private Vector3 syncEndPosition = Vector3.zero;

	void Start () {

		PhotonNetwork.sendRate = 20;
		PhotonNetwork.sendRateOnSerialize = 10;

		ipManager.addToItemInventory(0, 5);
		foreach(Weapons w in weaponManager.weapons)
		{
			if(w.isHolding)
			{
				weaponSpriteParent.sprite = w.weaponSprite;
				if(w.weaponType == WeaponType.Sword)
				{
					Sword sword = weaponSpriteParent.gameObject.AddComponent("Sword") as Sword;
					sword.minDamage = 15;
					sword.maxDamage = 25;
				}
			}
		}

		texWidth = 32;
		texHeight = 32;
		setupColours();
		changeSpritesColour();
	}

	void InputMovement (){
		if (Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.UpArrow)) {
			rigidbody.transform.position += Vector3.up * speed * Time.deltaTime;
			direction = 1;
		}
		if (Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.DownArrow)) {
			rigidbody.transform.position += Vector3.down * speed * Time.deltaTime;
			direction = 0;
		}
		if (Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.LeftArrow)) {
			rigidbody.transform.position += Vector3.left * speed * Time.deltaTime;
			direction = 2;
		}
		if (Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.RightArrow)) {
			rigidbody.transform.position += Vector3.right * speed * Time.deltaTime;
			direction = 3;
		}
		}

	void Update () 
	{
		if (ismine == false) {
			transform.position = syncEndPosition;
				}
		if (PhotonView.emeu) {
						InputMovement ();
				}
		else
		{
			SyncedMovement();
		}
		if(direction == 0)
		{
			spriteParent.sprite = forward;
			weaponSpriteParent.sortingOrder = 21;
			Quaternion newRot = Quaternion.Euler(0, 0, 0);
			weaponSpriteParent.transform.localRotation = newRot;
			weaponSpriteParent.transform.localPosition = forwardPos;
			changeSpritesColour();
		}
		if(direction == 1)
		{
			spriteParent.sprite = backward;
			weaponSpriteParent.sortingOrder = 19;
			Quaternion newRot = Quaternion.Euler(0, 0, 90);
			weaponSpriteParent.transform.localRotation = newRot;
			weaponSpriteParent.transform.localPosition = backPos;
			changeSpritesColour();
		}
		if(direction == 2)
		{
			spriteParent.sprite = left;
			weaponSpriteParent.sortingOrder = 19;
			Quaternion newRot = Quaternion.Euler(0, 0, 90);
			weaponSpriteParent.transform.localRotation = newRot;
			weaponSpriteParent.transform.localPosition = sidePos;
			changeSpritesColour();
		}
		if(direction == 3)
		{
			spriteParent.sprite = right;
			weaponSpriteParent.sortingOrder = 21;
			Quaternion newRot = Quaternion.Euler(0, 0, 0);
			weaponSpriteParent.transform.localRotation = newRot;
			weaponSpriteParent.transform.localPosition = sidePos;
			changeSpritesColour();
		}

		if(health <= 0)
			Die ();
	}


	public void Die()
	{
		print ("I've been killed");
	}

	public void addMoney(int am)
	{
		money = money + am;
	}

	public void takeMoney(int am)
	{
		money = money - am;
		if(money < 0)
			money = 0;
	}
	void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
	{
		if (stream.isWriting)
		{
			print ("writing");
			stream.SendNext(transform.position);
			stream.SendNext(rigidbody.position);
			stream.SendNext(rigidbody.velocity);
			ismine = true;
		}
		else
		{
			print ("Receiving");
			Vector3 syncPosition = (Vector3)stream.ReceiveNext();
			Vector3 syncVelocity = (Vector3)stream.ReceiveNext();
			
			syncTime = 0f;
			syncDelay = Time.time - lastSynchronizationTime;
			lastSynchronizationTime = Time.time;
			
			syncEndPosition = syncPosition + syncVelocity * syncDelay;
			syncStartPosition = rigidbody.position;
			ismine = false;
		}
	}
	private void SyncedMovement()
	{
		syncTime += Time.deltaTime;
		rigidbody.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
	}
}

[EDIT]
In my Instatiate.cs file i connect to photon and OnJoinedRoom() i put PhotonNetwork.Instatiate
My problem is solved thank you all ;)
Dinis Gomes

Comments

  • vadim
    Options
    Hi,

    How do you create instances? You should use PhotonNetwork.Instantiate for that (e.g., in OnJoinedRoom() handler).
    What is Entity, btw? Does it extend Photon.MonoBehaviour?
  • Tobias
    Options
    I can't debug your code, sorry.
    You should take a look at the Marco Polo Tutorial and actually work through it. It's 3d, yes, but the basics are the same. It covers basic setup of the prefab and instantiation and synchronization.
    http://doc.exitgames.com/en/pun/current ... marco-polo
  • vadim wrote:
    Hi,

    How do you create instances? You should use PhotonNetwork.Instantiate for that (e.g., in OnJoinedRoom() handler).
    What is Entity, btw? Does it extend Photon.MonoBehaviour?
    Yes, it extend. I have created a Instatiate.cs file that in void start() it have PhotonNetwork.Instatiate, i need to put that into OnJoinedRoom() ?

    Instatiate.cs
    using UnityEngine;
    using System.Collections;
    
    public class Instatiate : MonoBehaviour {
    
    	// Use this for initialization
    	void Start () {
    		GameObject player = PhotonNetwork.Instantiate("playerprefab", Vector3.zero, Quaternion.identity, 0);
    		Player playera = player.GetComponent<Player>();
    		playera.enabled = true;
    		camerafollow.target = player.transform;
    	}
    	
    	// Update is called once per frame
    	void Update () {
    	
    	}
    }
    
  • Tobias wrote:
    I can't debug your code, sorry.
    You should take a look at the Marco Polo Tutorial and actually work through it. It's 3d, yes, but the basics are the same. It covers basic setup of the prefab and instantiation and synchronization.
    http://doc.exitgames.com/en/pun/current ... marco-polo

    Hi Tobias, thanks for your reply.
    I had done that Tutorial multiple times already. Do i need to photonnetwork.instatiate inside of OnJoinedRoom?
    Thanks
  • Tobias
    Options
    You can't instantiate outside of a room, so please put PN.Instantiate into OnJoinedRoom(). I assume your script is started before you're in a room.
    Wouldn't that be faster to try it yourself than asking us? We're not the fastest to answer, after all.