Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Third Party Photon synth for Mobile controller

Discussion in 'Multiplayer' started by alarm656, Sep 8, 2014.

  1. alarm656

    alarm656

    Joined:
    Jul 6, 2013
    Posts:
    111
    Hi, I have spent a lot of times to get my mobile fps controller synch properly with Photon Network:(. There are no tutorials and I emailed to Exit games support but they can't help me((. All tutorials only for PC projects. Please, I'll happy for any help and feedback. You can PM me if you have something to offer me that we can discuis. Thank you again:)
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,018
    In an FPS you only apply the input to your character. Then you get a state "running", "crouching" etc. This should be sent, along with the position and rotation.
    This is more or less independent from the actual input-handling.
    You did have a look at the (non-first-person) Marco Polo Tutorial, right? You should be able to apply this to an FPS, too.
     
  3. alarm656

    alarm656

    Joined:
    Jul 6, 2013
    Posts:
    111
    By the way Marco polo is not mobile project and I need fps camera and another camera to see other players as a full model. these are what my player has
     

    Attached Files:

  4. alarm656

    alarm656

    Joined:
    Jul 6, 2013
    Posts:
    111
    My player has components: "Character Controller", "Movement Controller"-Script. To the player chiled Main Camera and just Capsule. Player controls with another object calls "Control Objects" and this "control object" object doesn't child to the Player object. There are no scripts attached to the "Control Objects" there are only GUI Textures.

    Movement Controller Script:

    #pragmastrict

    varcameraPivot : Transform;

    varspeed : float = 4;

    privatevarthisTransform : Transform;
    privatevarcharacter : CharacterController;
    privatevarcameraVelocity : Vector3;
    privatevarvelocity : Vector3;
    privatevarcanJump = true;

    varmovementOriginX : float;
    varmovementOriginY : float;

    functionStart ()
    {
    thisTransform = GetComponent(Transform);
    character = GetComponent(CharacterController);
    originalRotation = transform.rotation.eulerAngles.y;
    movePad.transform.position = newVector2(-1,-1);
    moveOutline.transform.position = newVector2(-1,-1);
    jump = false;
    doubleJump = false;
    }

    functionUpdate ()
    {
    varmoveDiff : Vector2;
    for (vartouch : TouchinInput.touches)
    {
    if (touch.phase == TouchPhase.Began)
    {
    if (jumpButton.HitTest(touch.position))
    {
    jump = true;
    }
    elseif (touch.position.x < Screen.width / 2)
    {
    leftFingerID = touch.fingerId;
    leftFingerCenter = touch.position;
    moveOutline.transform.position.x = touch.position.x / Screen.width;
    moveOutline.transform.position.y = touch.position.y / Screen.height;
    movePad.transform.position.x = touch.position.x / Screen.width;
    movePad.transform.position.y = touch.position.y / Screen.height;
    }
    else
    {
    rightFingerID = touch.fingerId;
    }
    }
    elseif (touch.phase == TouchPhase.Moved)
    {
    if (touch.position.x < Screen.width / 2)
    {
    if (leftFingerID == touch.fingerId)
    {
    mDiff = touch.position - leftFingerCenter;
    vardistPer = mDiff.magnitude * 100 / moveStickDiff;
    if (distPer > 100)
    {
    distPer = 100;
    }
    leftFingerInput = mDiff.normalized * distPer / 100;

    movePad.transform.position.x = leftFingerCenter.x / Screen.width + mDiff.normalized.x * distPer / 100 * moveStickDiff / Screen.width;
    movePad.transform.position.y = leftFingerCenter.y / Screen.height + mDiff.normalized.y * distPer / 100 * moveStickDiff / Screen.height;
    }
    }
    else
    {
    if (rightFingerID == touch.fingerId)
    {
    rightFingerInput = touch.deltaPosition * Time.smoothDeltaTime;
    }
    }
    }
    elseif (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
    {
    if (touch.fingerId == leftFingerID)
    {
    movePad.transform.position = newVector2(-1,-1);
    moveOutline.transform.position = newVector2(-1,-1);
    leftFingerID = -1;
    leftFingerInput = newVector2(0, 0);
    }
    if (touch.fingerId == rightFingerID)
    {
    rightFingerID = -1;
    rightFingerInput = newVector2(0, 0);
    }
    }
    }

    rotationX += rightFingerInput.x * 25;
    rotationY += rightFingerInput.y * 25;
    rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
    transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler(0, originalRotation + rotationX, 0), 0.1);
    cameraPivot.localRotation = Quaternion.Slerp (cameraPivot.localRotation, Quaternion.Euler(cameraPivot.localRotation.x-rotationY, 0, 0), 0.1);

    moveDirection = thisTransform.TransformDirection(newVector3(leftFingerInput.x, 0, leftFingerInput.y));
    moveDirection *= speed;
    moveDirection += Physics.gravity;

    if (character.isGrounded)
    {
    doubleJump = false;
    if (jump && jumpingEnabled)
    {
    velocityJ = character.velocity / 3;
    velocityJ.y = jumpSpeed;
    }
    else
    {
    velocityJ = newVector3(0, 0, 0);
    }
    }
    else
    {
    if (!doubleJump && jump && doubleJumpingEnabled)
    {
    velocityJ = character.velocity / 3;
    velocityJ.y = jumpSpeed;
    doubleJump = true;
    }
    velocityJ.y += Physics.gravity.y * Time.smoothDeltaTime;
    }

    moveDirection += velocityJ;

    character.Move(moveDirection * Time.smoothDeltaTime);
    jump = false;
    }

    varrightFingerID;
    varleftFingerID;
    varleftFingerCenter : Vector2;
    varmDiff : Vector2;
    varmoveStickDiff = 100;
    varleftFingerInput : Vector2;
    varrightFingerInput : Vector2;

    varmoveOutline : GUITexture;
    varmovePad : GUITexture;
    varjumpButton : GUITexture;

    varrotationX : float;
    varrotationY : float;
    varminimumY = -20;
    varmaximumY = 20;

    varoriginalRotation : float;
    varmoveDirection : Vector3;
    varjump : boolean;
    vardoubleJump : boolean;

    varjumpSpeed : float = 25;
    varvelocityJ : Vector3;
    vardoubleJumpingEnabled : boolean = true;
    varjumpingEnabled : boolean = true;
     
  5. alarm656

    alarm656

    Joined:
    Jul 6, 2013
    Posts:
    111
    What I changed: I add another camera call it "WeaponCamera" chiled under the MainCamera. Under "WeaponCamera" I chiled Arm and Weapon Model. Fixed firs person view camera clipping. Arm and weapon models are under "FPS" layer. MainCamera couldn't see the "fps" layer but WeaponCamera will see only "fps" layer. Dragged "Control Objects" under "Player" object [chiled] (that to have one full object). Than to the "Player" I have attached "Photon View", for Observe window I dragged "Transform component of the Player". Made a Prefab by dragging Player object to Project window. Placed it to the Resourses folder. using this script as a Network Manager:


    usingUnityEngine;
    usingSystem.Collections;

    publicclassNetworkManager : MonoBehaviour {

    publicCamerastandbyCamera;
    SpawnSpot[] spawnSpots;

    //Usethisforinitialization
    voidStart () {
    spawnSpots = GameObject.FindObjectsOfType<SpawnSpot>();
    Connect ();
    }

    voidConnect() {
    PhotonNetwork.ConnectUsingSettings( "MultiFPS v001" );
    }

    voidOnGUI() {
    GUILayout.Label( PhotonNetwork.connectionStateDetailed.ToString() );
    }

    voidOnJoinedLobby() {
    Debug.Log ("OnJoinedLobby");
    PhotonNetwork.JoinRandomRoom();
    }

    voidOnPhotonRandomJoinFailed() {
    Debug.Log ("OnPhotonRandomJoinFailed");
    PhotonNetwork.CreateRoom( null );
    }

    voidOnJoinedRoom() {
    Debug.Log ("OnJoinedRoom");

    SpawnMyPlayer();
    }

    voidSpawnMyPlayer() {
    if(spawnSpots == null) {
    Debug.LogError ("WTF?!?!?");
    return;
    }

    SpawnSpotmySpawnSpot = spawnSpots[ Random.Range (0, spawnSpots.Length) ];
    GameObjectmyPlayerGO = (GameObject)PhotonNetwork.Instantiate("MyPlayer", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
    //GameObjectmyPlayerGO1 = (GameObject)PhotonNetwork.Instantiate("FFP-GAME-MAIN", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);

    standbyCamera.enabled = false;

    ((MonoBehaviour)myPlayerGO.GetComponent("MovementController")).enabled = true;
    myPlayerGO.transform.FindChild("MainCamera").gameObject.SetActive(true);
    }
    }


    Ah yes I have 2 spawn points.

    everything is ok my players spawns. I can see my player as a first person view but I couldn't see second player. But they are in same server. I have to see other players like full 3D character model but I don't have a script and I dont know how to make it. But I have to see second player also like Arm and weapon?

    yes, I have disactivated MainCamera in "Player" prefab. but I have activated it in NetworkManager script.

    uhhh. I finished. Please help me
     
  6. alarm656

    alarm656

    Joined:
    Jul 6, 2013
    Posts:
    111
  7. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,018
    The prefab needs a PhotonView and this has to be setup. Drag and drop the Object's transform on the "Observed" field.
    This should sync positioning.
     
  8. alarm656

    alarm656

    Joined:
    Jul 6, 2013
    Posts:
    111
    Thank you for your answer but I did it early. Still having such as result
     
  9. alarm656

    alarm656

    Joined:
    Jul 6, 2013
    Posts:
    111
    No one understand me or everybody knows what I'm trying to do but doesn't want me help? I can't believe. if you search in this forum like "photon mobile controller" you can find only my post that you reading now. Please make this day my day. I'm so tired mentally((
     
  10. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    It's really bad form to post your entire scripts, especially when you don't use code blocks. Start by reading the stickies/forum guidelines. Likely people looked in here and went "I have better things to do than read all this unformatted code...". But from what I've seen you need to read through the exit games references more thoroughly. The photon framework works identically for PC and mobile. It's the input that is different. Once you understand photon you'll see that your solution is likely simple. Don't stress about it too much, just take your time and study up. The answer will come to you eventually. :)
     
    alarm656 likes this.
  11. Tiny-Tree

    Tiny-Tree

    Joined:
    Dec 26, 2012
    Posts:
    1,314
    Take the 4.6 assets sample rigidbody Fps controller, add the rigidbody interpolation in pun section from exit game forum, you don't need anything else. For Fps movement sync just reduce the data send rare to 6 or 8 it work better with this script
     
    alarm656 likes this.
  12. alarm656

    alarm656

    Joined:
    Jul 6, 2013
    Posts:
    111
    First of all I want to say Sorry for being Noob. Thank you for suggestion. It's need 4.6 version or higher version of Unity. Need to upgrade my Unity version. How 4.6 sample mobile controller controls? I mean old version asset's mobile prefab controlled only with joysticks. in my custom controller it's controls by screen. half side (Left Side) fully for movement and half (Right side) fully for rotation. You don't need touch to the joysticks to move or rotate. Joystick GUI texture invisible if you touch screen it's appears. you can control from every corner of the screen. Just from the center to the left side for moving and from the center to the right side for rotation)
     
  13. alarm656

    alarm656

    Joined:
    Jul 6, 2013
    Posts:
    111
    is 4.6 simple asset fps controller controls like my custom fps controller?
     
  14. alarm656

    alarm656

    Joined:
    Jul 6, 2013
    Posts:
    111
    Could you write more please. I took 4.6 assets. dragged simple FPS controller (RFPS) to the scene. Put "Sendrate" to 6.

    "add the rigidbody interpolation in pun section from exit game forum" - I don't understand(
     
  15. melihbahri

    melihbahri

    Joined:
    Jul 29, 2018
    Posts:
    1
    Unity Photon WASD working but Virtual Joystick won't work after another player has joined the network...? How can i sync joystick.
     
    Sachin49 likes this.
  16. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    As a guess, make sure the joystick script is only enabled for the local player.
     
    Joe-Censored likes this.
  17. viknesh2020

    viknesh2020

    Joined:
    Jul 19, 2016
    Posts:
    50
    How do I do that? I have been trying Photonview.isMine but the Joystick is not working when the second player joins.
     
  18. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    viknesh2020 likes this.
  19. viknesh2020

    viknesh2020

    Joined:
    Jul 19, 2016
    Posts:
    50
    Thank you for the reply. I am using Unity Standard asset third person controller (Ethan's model). For joystick i am using a script from asset store. Here is the link: https://assetstore.unity.com/packages/tools/input-management/joystick-pack-107631#content

    I have added a script to convert the vertical and horizontal movement of the player from keyboard input to joystick. Here is the script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5. using Photon.Realtime;
    6. using UnityStandardAssets.Characters.ThirdPerson;
    7.  
    8. public class ThirdPersonInput : MonoBehaviourPun
    9. {
    10.     public GameObject joypad;
    11.     public FixedJoystick fixedJoystick;
    12.     public FixedJoystick fixedJoystickRight;
    13.     public ThirdPersonUserControl control; //This is from the standard asset user input control which makes use of the keyboard.
    14.  
    15.     protected float cameraAngle;
    16.     public float cameraAngleSpeed;
    17.     public bool enableTouchField;
    18.  
    19.     [Tooltip("The local player instance. Use this to know if the local player is represented in the Scene")]
    20.     public static GameObject LocalPlayerInstance;
    21.  
    22.     public void Awake()
    23.     {
    24.         // #Important
    25.         // used in MPManager.cs: we keep track of the localPlayer instance to prevent instantiation when levels are synchronized
    26.         if (photonView.IsMine)
    27.         {
    28.             Instantiate(joypad);
    29.             ThirdPersonInput.LocalPlayerInstance = this.gameObject;
    30.             fixedJoystick = GameObject.Find("Fixed Joystick").GetComponent<FixedJoystick>();
    31.             fixedJoystickRight = GameObject.Find("Fixed Joystick Right").GetComponent<FixedJoystick>();
    32.             control = this.gameObject.GetComponent<ThirdPersonUserControl>();
    33.         }
    34.         // #Critical
    35.         // we flag as don't destroy on load so that instance survives level synchronization, thus giving a seamless experience when levels load.
    36.         DontDestroyOnLoad(this.gameObject);
    37.  
    38.     }
    39.  
    40.     void Start()
    41.     {
    42.         control = GetComponent<ThirdPersonUserControl>();
    43.     }
    44.  
    45.     // Update is called once per frame
    46.     void Update()
    47.     {
    48.  
    49.         if (photonView.IsMine)
    50.         {
    51.             control.hInput = fixedJoystick.Horizontal;  //Converting the keyboard input to the joystick movement
    52.             control.vInput = fixedJoystick.Vertical;
    53.  
    54.             if (enableTouchField)
    55.             {
    56.              //Camera control goes here.
    57.                 cameraAngle += fixedJoystickRight.input.x * cameraAngleSpeed;
    58.              
    59.                 Camera.main.transform.position = transform.position + Quaternion.AngleAxis(cameraAngle, Vector3.up) * new Vector3(0, 3, 3);
    60.                 Camera.main.transform.rotation = Quaternion.LookRotation(transform.position + Vector3.up * 2f - Camera.main.transform.position, Vector3.up);
    61.             }
    62.  
    63.         }
    64.     }
    65. }

    Two joypads on screen has been used. One is for player movement and the other is for camera movement. They both are placed as a child of a canvas and it will be instantiated as a prefab. The player prefab has the above script along with standard asset scripts for the third person controller.

    Hope i covered whatever is needed to rectify the issue. Please let me know if you need any clarification.
     
  20. viknesh2020

    viknesh2020

    Joined:
    Jul 19, 2016
    Posts:
    50
    Screenshot of the inspector window for Player prefab:

    Screen Shot 2020-01-31 at 6.49.40 PM.png
     
  21. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    I'd be inclined to disable or remove the input controller scripts in the OnPhotonInstantiate callback function where photonView.IsMine is false. That way you can be certain that the player object will only be controlled by the client that owns it.
     
  22. viknesh2020

    viknesh2020

    Joined:
    Jul 19, 2016
    Posts:
    50
    Thank you. Finally it works as expected but with huge lag while updating player movements (out of topic for this specific forum). The following change to the script did the trick for joypad.

    Code (CSharp):
    1. public void Awake()
    2.     {
    3.         // #Important
    4.         // used in MPManager.cs: we keep track of the localPlayer instance to prevent instantiation when levels are synchronized
    5.         if (photonView.IsMine)
    6.         {
    7.             GameObject jpad = Instantiate(joypad) as GameObject;
    8.             ThirdPersonInput.LocalPlayerInstance = this.gameObject;
    9.             jpad.transform.parent = this.gameObject.transform;
    10.             fixedJoystick = GameObject.Find("Fixed Joystick").GetComponent<FixedJoystick>();
    11.             fixedJoystickRight = GameObject.Find("Fixed Joystick Right").GetComponent<FixedJoystick>();
    12.             fixedButton = GameObject.Find("Handle").GetComponent<FixedButton>();
    13.             control = this.gameObject.GetComponent<ThirdPersonUserControl>();
    14.         }
    15.         // #Critical
    16.         // we flag as don't destroy on load so that instance survives level synchronization, thus giving a seamless experience when levels load.
    17.         DontDestroyOnLoad(this.gameObject);
    18.  
    19.     }
     
    Sachin49, tobiass and Munchy2007 like this.