For a very simple, minimalistic unity game I developed myself, I am trying to retrieve the positions for every player. I am very new to both C# and Unity.
I am following this tutorial: https://github.com/ethanedits/Universal-Unity-ESP
(disclaimer I am not trying to develop this in any way that would make me gain an unfair advantage in any multiplayer game. This is strictly for personal education purposes).
I'm trying to identify the right name of the Player objects in order to get their positions with transform() in the hax.cs
file which originally is like this:
namespace Universal_Unity_ESP
{
class Hacks : MonoBehaviour
{
public void OnGUI()
{
foreach (Player player in FindObjectsOfType(typeof(Player)) as Player[])
{
//In-Game Position
Vector3 pivotPos = player.transform.position; //Pivot point NOT at the feet, at the center
Problem is, when I made the game, I didn't declare a player class per se. I am using the Photon add-in, so my player object in Unity is within the Assets/resources folder.
For me, the player is just declared as an object contained in the RoomManager class, and I'm not sure how I should adapt the above code. This is what I tried, with the errors in comments.
using UnityEngine;
using Photon.Realtime;
namespace Universal_Unity_ESP
{
class Hacks : MonoBehaviour
{
public void OnGUI()
{
foreach (Player player in FindObjectsOfType(typeof(Player)) as Player[]) //ERROR: cannot convert type unityengine.object to Photon.realtime.player
{
//In-Game Position
Vector3 pivotPos = player.transform.position; //ERROR: player does not contain a definition for transform
My roomManager.cs script is below with two underlined parts. As you can see the GameObject player
is an object within the roomManager
and not a Class by itself. It seems to be instantiated through PhotonNetwork.Instantiate, which does not inherit from Monobehaviour, meaning I'm not able to retrieve its position in the above code (for which I also had to be using photon.realtime
, which probably isn't the right approach).
There is also a Player.cs file in \Assets\Photon\PhotonRealtime\Code\Player.cs file) but it also does not inherit from Monobehaviour and I don't think that this is the right approach. Any help more than welcome.
My RoomManager.cs code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class RoomManager : MonoBehaviourPunCallbacks
{
public static RoomManager instance;
public GameObject player;
[Space]
public Transform[] spawnPoints;
[Space]
public GameObject roomCam;
[Space]
public GameObject nameUI;
public GameObject connectingUI;
private string nickname = "unnamed";
void Awake() {
instance = this;
}
public void ChangeNickname(string _name) {
nickname = _name;
}
public void JoinRoomButtonPressed() {
Debug.Log("Connecting...");
PhotonNetwork.ConnectUsingSettings();
nameUI.SetActive(false);
connectingUI.SetActive(true);
}
// Start is called before the first frame update
void Start()
{
}
public override void OnConnectedToMaster()
{
base.OnConnectedToMaster();
Debug.Log("Connected to Server");
PhotonNetwork.JoinLobby();
}
public override void OnJoinedLobby() {
base.OnJoinedLobby();
PhotonNetwork.JoinOrCreateRoom("test", null, null);
Debug.Log("Connected, in the Lobby");
}
public override void OnJoinedRoom() {
base.OnJoinedRoom();
Debug.Log("Connected, in a room");
roomCam.SetActive(false);
SpawnPlayer();
}
public void SpawnPlayer() {
Transform spawnPoint = spawnPoints[UnityEngine.Random.Range(0,spawnPoints.Length)];
GameObject _player = PhotonNetwork.Instantiate(player.name, spawnPoint.position, Quaternion.identity);
_player.GetComponent<PlayerSetup>().IsLocalPlayer();
_player.GetComponent<Health>().isLocalPlayer = true; // setting the attribute to true in the health script
_player.GetComponent<PhotonView>().RPC("SetNickname", RpcTarget.AllBuffered, nickname);
}
}