- Home /
photon player movement
Hi guys my name is cuddle bunny and me and a few friends are working on a 2d top down social mmo like club pengrin but we have hit a wall and none of us can figure out how to fix it Here is are movement scrip
using UnityEngine;
using System.Collections;
public class PlayerMovement :MonoBehaviour
{
public float speed = 1f;
void Start()
{
}
void Update()
{
if(Input.GetKey(KeyCode.D))
transform.position += new Vector3(speed *Time.deltaTime,0.0f,0.0f);
if(Input.GetKey(KeyCode.A))
transform.position -= new Vector3(speed *Time.deltaTime,0.0f,0.0f);
if(Input.GetKey(KeyCode.W))
transform.position += new Vector3(0.0f, speed *Time.deltaTime,0.0f);
if(Input.GetKey(KeyCode.S))
transform.position -= new Vector3(0.0f, speed *Time.deltaTime,0.0f);
}
}
the problem we are having is wean some one move their player it move every one else on the person screen but on every one else screen that person dint mover every one and is moved to the rite place that they wanted to go the only way we have found to fix this is to have the other player move to up date the others screen but then that person get the bug how can we fix this were it doesn't do this any more
Have u added Photon View onto your player??? Photon View syncs the players screens with others. So they see what happens.
Answer by ryan_unity · Feb 10, 2015 at 03:42 PM
Hey Cuddle Bunny,
Make sure there is a PhotonView attached to each player and use photonView.isMine to make sure you only control your own player.
void Start() {
enabled = photonView.isMine;
}
OR
void Update() {
if (photonView.isMine) {
// movement code
}
}
This is a common problem newcomers have when creating a multiplayer game: http://answers.unity3d.com/questions/313706/controlling-the-wrong-client-with-photon-cloud.html http://answers.unity3d.com/questions/289119/network-problem-moving-the-wrong-player.html http://answers.unity3d.com/questions/439600/photonview-controlling-multiple-fps-controllers.html
I hope this helps :)
um thanks ther one more qretshon why is it saying that
Assets/playermovement.cs(10,27): error CS0103: The name `photonView' does not exist in the current context
her is my script
using UnityEngine;
using System.Collections;
public class Player$$anonymous$$ovement :$$anonymous$$onoBehaviour
{
public float speed = 1f;
void Start()
{
enabled = photonView.is$$anonymous$$ine;
}
void Update(){
if (photonView.is$$anonymous$$ine) {
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
transform.position += new Vector3(speed *Time.deltaTime,0.0f,0.0f);
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
transform.position -= new Vector3(speed *Time.deltaTime,0.0f,0.0f);
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
transform.position += new Vector3(0.0f, speed *Time.deltaTime,0.0f);
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.S))
transform.position -= new Vector3(0.0f, speed *Time.deltaTime,0.0f);
}
}
}
There are multiple ways to fix this, for instance changing the inheritance:
public class Player$$anonymous$$ovement : Photon.$$anonymous$$onoBehaviour {
Or just declaring this and linking it in the inspector:
public PhotonView photonView;
It seems you might be getting confused by some basic program$$anonymous$$g and Unity principles. I highly recommend checking out some tutorials and perhaps making some simpler games/projects before an $$anonymous$$$$anonymous$$O. It will make program$$anonymous$$g a lot easier :-)
Useful links: - http://www.3dbuzz.com/ - http://doc.exitgames.com/en/pun/current/tutorials/photon-unity-and-networking-links - https://unity3d.com/learn/tutorials/modules/beginner/scripting
Answer by mast3rillusion · Feb 20, 2015 at 05:14 AM
This-
public class PlayerMovement :MonoBehaviour
needs to be this-
public class PlayerMovement :Photon.MonoBehaviour
also u need to add a new method-
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) { if (stream.isWriting) stream.SendNext(rigidbody.position); else rigidbody.position = (Vector3)stream.ReceiveNext(); }
here is a good tutorial so u can understand better.
Your answer
Follow this Question
Related Questions
Photon Network Movement Delay 1 Answer
Photon Networking Player Control Issue 1 Answer
Bug. Player can't move. 3 Answers