How to get the position of all the player in Photon - c#

I want to know how to get the position of all available player in the network.
So how can I update my script ?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Pathfinding : MonoBehaviour {
Transform[] Players;
int TotalPlayerCount=0;
void Update() {
foreach (PhotonPlayer pl in PhotonNetwork.playerList) {
if (GetComponent<PhotonView> ().isMine) {
Players[TotalPlayerCount].position= //position of my player
TotalPlayerCount++;
}
else
{
Players[TotalPlayerCount].position=//position of all other player available in the room
TotalPlayerCount++;
}
}
}

Each client can set it's player's custom properties with SetCustomProperties, even before being in a room. They are synced when joining a room.
Hashtable xyPos = new Hashtable();
xyPos.Add(1, "10");
xyPos.Add(2, "-20");
PhotonPlayer.SetCustomProperties(xyPos, null, true) ;
For further reading, you should try following tutorial(It will give you a stronger idea how to get and sync positions):
http://doc.photonengine.com/en/pun/current/tutorials/tutorial-marco-polo#_Toc317517599

Related

Accessing centerEyeRotation (face tracking) from TrackedPoseDriver in Unity

I am using ARKit within Unity to implement face tracking on an iOS device. Face tracking is working fine, and I want to access the real-time centerEyeRotation measurements produced by the face tracking. Following this helpful post
I used the code below to try and at least access the TrackedPoseDriver, but I keep getting back the data from the Main Camera (the game object that this script is attached to). Any idea how I can access the centerEyeRotation data?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class getTrackedPoseDriver : MonoBehaviour
{
bool found;
MonoBehaviour mbTrackingPose;
MonoBehaviour mb;
// Start is called before the first frame update
void Start()
{
Component[] components = Camera.main.GetComponents(typeof(MonoBehaviour));
found = false;
foreach (Component component in components)
{
string name = component.ToString();
mb = (MonoBehaviour)component;
if (name.Contains("UnityEngine.InputSystem.XR.TrackedPoseDriver"))
{
mbTrackingPose = mb;
found = true;
Debug.Log(found);
Debug.Log(mbTrackingPose.name);
break;
}
}
}
// Update is called once per frame
void Update()
{
Debug.Log(mbTrackingPose.transform.rotation.y);
}
}
I also attempted a different approach (see below). This script (attached to the Main Camera) does confirm XRNode.CenterEye as valid, but device.TryGetFeatureValue fails to get centerEyePosition.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
public class centerEyeRotation : MonoBehaviour
{
Quaternion centEyeRot;
Vector3 centEyePos;
//Quaternion rotation;
bool tryGet;
// Start is called before the first frame update
void Start()
{
tryGet = false;
InputDevice device = InputDevices.GetDeviceAtXRNode(XRNode.CenterEye);
if (device.isValid)
{
Debug.Log("Device is valid");
if (device.TryGetFeatureValue(CommonUsages.centerEyePosition, out centEyePos))
tryGet = true;
Debug.Log("centerEyePosition found");
}
}
}
I may have solved the problem via a different route; the project instantiates an AR face prefab object at runtime. By attaching a script to this prefab I was able to report the transform of the prefab, which seems to be giving me the orientation data I need.

Why does setting my Rotate.z change all variables?

Im trying to make a sprite rotate onscreen to show the player how their Jet is rotated while they fly.
Here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AircraftScript : MonoBehaviour
{
public float Altitude;
public Text AltitudeText;
public GameObject AircraftImage;
public GameObject Aircraft;
void Update()
{
Altitude = Aircraft.GetComponent<Rigidbody>().position.y;
AltitudeText.text = Altitude + "ft";
AircraftImage.Rotate.z = Aircraft.Rotate.z;
}
}
It changed all the values of the Sprite (x,y,z) instead of just the z.
I did also try
AircraftImage.Rotate = new Vector3(0 , 0 , Aircraft.Rotate);
and
AircraftImage.Rotate = new Vector3(0 , 0 , Aircraft.Rotate.z);
neither worked, what have I done wrong?
I don't understand your code: Aircraft and AircraftImage are GameObjects, and GameObjects don't have the Rotate method, so that code should throw an exception.
Anyway I try to help you anyway.
Try using this code:
AircraftImage.GetComponent<RectTransform>().eulerAngles = new Vector3(0, 0, Aircraft.transform.eulerAngles.z);

Using Photon in Unity not in sync with the players

I have recently started using Photon to create multiplayer games, my tutorial is linked here: https://www.youtube.com/watch?v=93SkbMpWCGo . As you can read from the title of this problem, the players movements are not syncing.
here if the code for my player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using Photon.Pun;
public class playerControls : MonoBehaviour
{
[SerializeField] private Rigidbody2D body;
[SerializeField] public Transform bodyTransform;
public float speed;
private float horizontalInput;
public PhotonView view;
// Update is called once per frame
void Update()
{
if(view.IsMine){
if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
{
bodyTransform.localScale = new Vector3(1.3f,1.3f,1.3f);
}
else if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
{
bodyTransform.localScale = new Vector3(-1.3f,1.3f,1.3f);
}
horizontalInput = Input.GetAxis("Horizontal");
body.velocity = new Vector2(horizontalInput * (float)speed, body.velocity.y);
if(Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W)){
body.velocity=new Vector2(body.velocity.x,6);
}
}
}
}
I have also used the Photon Transform View, however it still does not seem to work.
Everything else works fine, you can move each player individually. You can join rooms. And the players spawn. The only problem however is just that the player sync does not work. Any feedback would be appreceated -Jake
Sidenote it is playable here: https://chartreusefinancialdifferences.jake-is-theis.repl.co/
PhotonTransformView is just a component that can be synced over the network. You also need PhotonView that does actual sync and drag-and-drop your PhotonTransformView to the Observed Components list. So basically the PhotonView is most important component for networking, PhotonTransformView is just a serializing/deserializing values from transform. If you add any class implementing IPunObservable you need to drag them to the Observed Components list to work

cant move my object to correct position on scene

Hi am using this code to try and move the object when triggered collision.
the following code seems to move the object off the scene to different x/y values.
The result I'm looking for is the object to move to the positions listed below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fairy : MonoBehaviour {
private void OnTriggerEnter2D(Collider2D collision)
{
//Debug.Log("Collision");
if (collision.gameObject.tag == "T")
{
collision.gameObject.transform.position =new Vector3 (-2.51f,-2.56f,0f);
Debug.Log("T Position is " + gameObject.transform.position);
}
}
}

I'm trying to declare a null on an array that at one point in the game no longer exists

I'm trying to apply a null on an array. The arrays are points where game prefabs are being instantiated. At some point in the game, these spawn points may be destroyed. When this happens, the code is still trying to access that spawn point even though it no longer exists, so I'm trying to make it null. So far I have been unsuccessful. This is the last thing in the game I need to fix. Help is appreciated.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
public class podControl : MonoBehaviour {
public Transform [] spawns;
public float spawnTime = 6f;
public float secondSpawnTime = 3f;
public GameObject podPrefab;
void Start ()
{
InvokeRepeating ("landingPod", spawnTime, secondSpawnTime);
}
void landingPod ()
{
int spawnIndex = Random.Range (0, spawns.Length);
if (spawns != null) {
Instantiate (podPrefab, spawns [spawnIndex].position, spawns [spawnIndex].rotation);
}
}
}
shouldn't it be
if(spawns[spawnIndex] != null)
Otherwise it's checking to see if the array is not null rather than the entry

Categories

Resources