Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Mbahri58 · Sep 11, 2018 at 01:44 PM · networkingnetworkphotonjoysticknetworkplayer

Unity Photon WASD working but Virtual Joystick won't work after another player has joined the network...? How can i sync joystick.

`**PlayerController public class PlayerController : MonoBehaviour { //Photon.MonoBehaviour private GameObject sceneCam; public GameObject plCamera; public GameObject joystick; public GameObject pistolObject; public float walkSpeed = 30f; Rigidbody2D _rigidbody2D; Vector2 rot; Vector2 vector; Quaternion rotation; PhotonView photonView;

 void Start () 
 {
     
 }

 void Awake()
 {
     _rigidbody2D = gameObject.GetComponent<Rigidbody2D>();
     photonView = GetComponent<PhotonView>();
     _tr = gameObject.GetComponent<Transform>();

     if(photonView.isMine)
     {
     sceneCam = GameObject.Find("MainCamera");

     //joystick = GameObject.Find("JoystickCanvas");
     //joystick.SetActive(true);
     //sceneCam.SetActive(false);
     //plCamera.SetActive(true);
     GetComponent<PlayerController>().enabled = true;
     }
     photonView = GetComponent<PhotonView>();
 }

 void Update () 
 {
     Movement();
     if (photonView.isMine)
     {
     Debug.Log("photonView Working");
     }
 }

 void FixedUpdate()
 {
     Movement();
     if(photonView.isMine == false )
     {
         return;
     }
     Move(vector, rotation);
     Rotate(rotation);
 }

private void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) { if(stream.isWriting) { Debug.Log("OnPhotonSerializeView"); stream.SendNext(transform.position); stream.SendNext(transform.rotation); stream.SendNext(walkSpeed); stream.SendNext(_rigidbody2D.position); stream.SendNext(_rigidbody2D.rotation); stream.SendNext(_rigidbody2D.velocity); } else { Debug.Log("OnPhotonSerializeView"); vector = (Vector3)stream.ReceiveNext(); rotation = (Quaternion)stream.ReceiveNext(); walkSpeed = (float)stream.ReceiveNext();

     transform.position = (Vector3)stream.ReceiveNext ();
     transform.rotation = (Quaternion)stream.ReceiveNext ();
 }

}

 public void Move(Vector2 vector, Quaternion rotation)
 {
     
     
     rot = vector ;
     if (rot != Vector2.zero)
         _rigidbody2D.AddForce(rot * walkSpeed, ForceMode2D.Force);

     gameObject.transform.rotation = rotation;
     
 }

 public void Rotate(Quaternion rotation)
 {
     pistolObject.transform.rotation = rotation;
 }

JOYSTICK CODE

**using UnityEngine; using System.Collections; using System.Collections.Generic;

public class TouchController : MonoBehaviour {

[HideInInspector] public PlayerController playerController; public LeftJoystick leftJoyStick; public RightJoystick rightJoyStick;

 void Start () {
     Input.multiTouchEnabled = true;
 }
 void Update () {
     MovingControllerUpdate();
     RotatingControllerUpdate();
 }
 public void MovingControllerUpdate() {

     Vector2 p = leftJoyStick.GetInputDirection();
     if ((p.x > -0.2f && p.x < 0.2) && (p.y > -0.2f && p.y < 0.2))
         return;

     float angle = Mathf.Atan(p.y / p.x) * Mathf.Rad2Deg;
     if (p.x < 0)
     {
         if (p.y < 0)
         {
             angle -= 180;
         }
         else
         {
             angle += 180;
         }
     }
     //playerController.Shoot();

     //Debug.Log("Angle : " + angle + " sw : " + Screen.width + " mw : " + Input.mousePosition.x);

     playerController.Move(leftJoyStick.GetInputDirection(), Quaternion.AngleAxis(angle, Vector3.forward)); 
 }
 public void RotatingControllerUpdate()
 {
     Vector2 p = rightJoyStick.GetInputDirection();
     if ((p.x > -0.2f && p.x < 0.2) && (p.y > -0.2f && p.y < 0.2))
         return;
     
     float angle = Mathf.Atan(p.y / p.x) * Mathf.Rad2Deg;
     if (p.x < 0)
     {
         if (p.y < 0)
         {
             angle -= 180;
         }
         else
         {
             angle += 180;
         }
     }
     //playerController.Shoot();

     //Debug.Log("Angle : " + angle + " sw : " + Screen.width + " mw : " + Input.mousePosition.x);
     playerController.Rotate(Quaternion.AngleAxis(angle, Vector3.forward));
     

 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ChristianSimon · Sep 11, 2018 at 02:28 PM

Hi,

I'm not sure how to describe the problems I have seen in the code snippets, so I'm trying to describe how you can set up the scripts to make it work.

First thing is, that you should check the isMine condition of the PhotonView component, whenever you handle Input. An example related to your code snippets:

 public void Update()
 {
     if (!photon.isMine)
         return;
 
     Move();
     Rotate();
 }

Checking the isMine condition makes sure, that only Input from the Owner of the object gets processed.

The next is about synchronizing the object. Here you basically have two options. The first one is - as far as I can see - what you tried in the first code snippet: using RPCs. When using this approach, you are using the PhotonView component and call the RPC(...) function which calls a certain function in the same script on the same object. When using PUN, this function needs to have the [PunRPC] attribute. An example:

 // RPC somewhere in code
 photonView.RPC("DoSomething", PhotonTargets.Others);

 // Function that gets called
 [PunRPC]
 public void DoSomething() { }

This way you basically can do the synchronization. There is however a more elegant way to do this by adding a PhotonTransformView component (or a custom OnPhotonSerializeView solution) which is observed by the PhotonView component. Using the PhotonTransformView component also has the advantage of integrated Inter- and Extrapolation options you can take a look at.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mbahri58 · Sep 11, 2018 at 03:09 PM 0
Share

Already i am using PhotonView, Photon Transform View, Rigidbody 2d View and i tried OnPhotonSerializeView but not worked

avatar image ChristianSimon Mbahri58 · Sep 11, 2018 at 03:14 PM 0
Share

Is the PhotonView component observing the *View components? Here is an example how this works.

avatar image Mbahri58 ChristianSimon · Sep 11, 2018 at 03:19 PM 0
Share

$$anonymous$$an, wasd and joystick everything working but Joystick won't work after another player has joined the network

avatar image
0

Answer by stnaing · Mar 21, 2019 at 08:29 AM

in my case , playerController become null after other player joined. It happened because current player object is refreshed by new joined player object. there is work around that check playerController is null before use . if null add reference of playerController again.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

222 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I sync Rigidbody2D over the network? 0 Answers

i want to create a custom network lobby manager. How can i script it? 0 Answers

How can I continuously receive the location of game object from 000webhost database? 0 Answers

i can't see other player with photon cloud server,I Can't see each other player in photon cloud 2 Answers

Get name/variables from previously on server players 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges