Transport gameobjects with photon
Hello, han... i come through the unity forum, to ask for help, because i am having a lot of difficulty fixing my error. i am building a prototype minion mobas, and i am having difficulty transporting to my entity, the list of gameobjects for it to follow;
this is my wave manager
using Photon.Pun;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class WaveManager : MonoBehaviourPun
{
PhotonView photon;
public int TeamID = 0;//0 = Blue Team, 1 = Red Team;
public List<GameObject> SpawnPoints;
public List<Lane> LaneSpawnPoints;
public Transform Minions, Flechas;
public string MeleePrefab;
public string RangePrefab;
public string CannonPrefab;
public string SuperPrefab;
public int WaveNumber = 0;
public float waveTimer = 0;
public bool MidInibidor = false;
public bool TopInibidor = false;
public bool BotInibidor = false;
void Start()
{
photon = GetComponent<PhotonView>();
waveTimer = GameConsts.MINION_WAVE_TIME;
}
// Update is called once per frame
void Update()
{
photon.RPC("SpawnWave", RpcTarget.AllBufferedViaServer);
}
[PunRPC]
void SpawnWave()
{
if (InGameManager.Instance.GameTime < GameConsts.MINION_SPAWN_TIME)
return;
if (waveTimer > GameConsts.MINION_WAVE_TIME)
{
System.TimeSpan t = System.TimeSpan.FromSeconds(InGameManager.Instance.GameTime);
Debug.Log(String.Format("Quantidade de Tropas: {0} foi instanciada em {1}!",
WaveNumber,
string.Format("{0:D2}:{1:D2}", t.Minutes, t.Seconds)));
if(BotInibidor)
{
for (int m = 0; m < GameConsts.MELEE_COUNT; m++)
{
SpawnUnit(SuperPrefab, GameConsts.SPAWN_BOT);
}
}
if (MidInibidor)
{
for (int m = 0; m < GameConsts.MELEE_COUNT; m++)
{
SpawnUnit(SuperPrefab, GameConsts.SPAWN_MID);
}
}
if (TopInibidor)
{
for (int m = 0; m < GameConsts.MELEE_COUNT; m++)
{
SpawnUnit(SuperPrefab, GameConsts.SPAWN_TOP);
}
}
for (int m = 0; m < GameConsts.MELEE_COUNT; m++)
{
photon.RPC("SpawnUnit", RpcTarget.AllBufferedViaServer, MeleePrefab, GameConsts.SPAWN_MID);
photon.RPC("SpawnUnit", RpcTarget.AllBufferedViaServer, MeleePrefab, GameConsts.SPAWN_TOP);
photon.RPC("SpawnUnit", RpcTarget.AllBufferedViaServer, MeleePrefab, GameConsts.SPAWN_BOT);
}
for (int m = 0; m < GameConsts.RANGE_COUNT; m++)
{
photon.RPC("SpawnUnit", RpcTarget.AllBufferedViaServer, RangePrefab, GameConsts.SPAWN_MID);
photon.RPC("SpawnUnit", RpcTarget.AllBufferedViaServer, RangePrefab, GameConsts.SPAWN_TOP);
photon.RPC("SpawnUnit", RpcTarget.AllBufferedViaServer, RangePrefab, GameConsts.SPAWN_BOT);
}
waveTimer = 0;
WaveNumber++;
}
else
{
waveTimer += Time.deltaTime;
}
}
[PunRPC]
void SpawnUnit(string prefab, int spawnLoc)
{
GameObject go = PhotonNetwork.Instantiate(Path.Combine("Minions", prefab),
SpawnPoints[spawnLoc].transform.position,
Quaternion.identity, 1);
go.transform.SetParent(Minions);
Tropa tropa = go.GetComponent<Tropa>();
tropa.GetComponent<PhotonView>().RPC("ChangePath", RpcTarget.AllBufferedViaServer, InGameManager.MakePath(TeamID, spawnLoc));
//tropa.target = tropa.Path[0].transform;
}
}
[System.Serializable]
public class Lane
{
public List<GameObject> Waypoints;
}
this is my tropas
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.AI;
public class Tropa : MonoBehaviour
{
PhotonView photon;
public Transform target;
public Transform EnemyTarget;
public float rangeEnemyDetect = 8;
NavMeshAgent navMeshAgent;
Animator animator;
public float DelayAttack;
float TimerForNextAttack;
float rangeAttack;
public enum Team
{
Blue,
Red
}
public Team team;
[HideInInspector] public string currentTeam;
[Header("Projéteis")]
public Transform SpawnProjectille;
public GameObject Projectille;
public int PathCount = 0;
public float dist;
public List<GameObject> Path;
public enum AttackType
{
Melee,
Range,
Cannon
}
public AttackType attackType;
public List<Tropa> Enemy = new List<Tropa>();
public WaveManager waveManager;
Vector3 RelativePos;
void Start()
{
photon = GetComponent<PhotonView>();
animator = GetComponent<Animator>();
navMeshAgent = GetComponent<NavMeshAgent>();
TimerForNextAttack = 0.01f;
switch (team)
{
case Team.Blue:
currentTeam = "Blue";
break;
case Team.Red:
currentTeam = "Red";
break;
}
}
// Update is called once per frame
void Update()
{
photon.RPC("DetectEnemy", RpcTarget.AllBufferedViaServer);
photon.RPC("SetAttackType", RpcTarget.AllBufferedViaServer);
photon.RPC("AttackOrMove", RpcTarget.AllBufferedViaServer);
}
[PunRPC]
void AttackOrMove()
{
if (navMeshAgent.velocity.magnitude > 0)
{
animator.SetBool("IsMoving", true);
}
else
{
animator.SetBool("IsMoving", false);
}
if (EnemyTarget != null)
{
RelativePos = new Vector3(EnemyTarget.position.x, transform.position.y, EnemyTarget.position.z);
if (Vector3.Distance(transform.position, EnemyTarget.position) <= rangeAttack)
{
transform.LookAt(RelativePos);
navMeshAgent.ResetPath();
if (TimerForNextAttack > 0)
{
TimerForNextAttack -= Time.deltaTime;
}
else if (TimerForNextAttack <= 0)
{
photon.RPC("Attack", RpcTarget.AllBufferedViaServer);
TimerForNextAttack = DelayAttack;
}
}
}
else
{
dist = navMeshAgent.remainingDistance;
EnemyTarget = null;
navMeshAgent.destination = target.position;
if (target != null)
navMeshAgent.SetDestination(target.position);
if (navMeshAgent.remainingDistance < navMeshAgent.stoppingDistance)
{
PathCount++;
if (PathCount < Path.Count)
target = Path[PathCount].transform;
}
}
}
[PunRPC]
public void UpdatePath(List<GameObject> path)
{
Path = path;
}
[PunRPC]
public void UpdateWave(WaveManager wave)
{
waveManager = wave;
}
[PunRPC]
void SetAttackType()
{
switch (attackType)
{
case AttackType.Melee:
rangeAttack = 2;
Projectille = null;
SpawnProjectille = null;
break;
case AttackType.Range:
rangeAttack = 10;
break;
case AttackType.Cannon:
rangeAttack = 5;
break;
}
}
[PunRPC]
void DetectEnemy()
{
if (currentTeam == "Blue")
{
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Minions Red");
float shortestDistance = Mathf.Infinity;
GameObject nearsetEnemy = null;
foreach (GameObject enemy in enemies)
{
float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
if (distanceToEnemy < shortestDistance)
{
shortestDistance = distanceToEnemy;
nearsetEnemy = enemy;
}
}
if (nearsetEnemy != null && shortestDistance <= rangeEnemyDetect)
{
EnemyTarget = nearsetEnemy.transform;
}
else
{
EnemyTarget = null;
}
}
else
{
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Minions Blue");
float shortestDistance = Mathf.Infinity;
GameObject nearsetEnemy = null;
foreach (GameObject enemy in enemies)
{
float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
if (distanceToEnemy < shortestDistance)
{
shortestDistance = distanceToEnemy;
nearsetEnemy = enemy;
}
}
if (nearsetEnemy != null && shortestDistance <= rangeAttack)
{
EnemyTarget = nearsetEnemy.transform;
}
else
{
EnemyTarget = null;
}
}
}
[PunRPC]
void Attack()
{
animator.SetTrigger("IsAttacking");
photon.RPC("Shot", RpcTarget.AllBufferedViaServer);
}
[PunRPC]
void Shot()
{
GameObject arrow = PhotonNetwork.Instantiate("Creep Arrow", SpawnProjectille.position, SpawnProjectille.rotation);
arrow.GetComponent<PhotonView>().RPC("UpdateTarget", RpcTarget.AllBufferedViaServer, EnemyTarget);
arrow.transform.SetParent(waveManager.Flechas);
}
[PunRPC]
public void ChangePath(List<GameObject> path)
{
Path = path;
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
stream.SendNext(animator);
stream.SendNext(animator.GetBool("IsMoving"));
}
else
{
transform.position = (Vector3)stream.ReceiveNext();
transform.rotation = (Quaternion)stream.ReceiveNext();
animator = (Animator)stream.ReceiveNext();
animator.SetBool("IsMoving", (bool)stream.ReceiveNext());
}
}
}
and this my GameManager
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using System.Timers;
using UnityEngine;
using UnityEngine.UI;
public class InGameManager : MonoBehaviour
{
public static InGameManager Instance;
public float GameTime;
public Text timeCounter;
// 0 = Mid, 1 = Top, 2 = Bot;
PhotonView photon;
public List<WaveManager> Teams;
void Awake()
{
if (Instance == null)
Instance = this;
//photon = GetComponent<PhotonView>();
}
// Update is called once per frame
void Update()
{
GameTime += Time.deltaTime;
System.TimeSpan t = System.TimeSpan.FromSeconds(GameTime);
timeCounter.text = string.Format("{0:D2}:{1:D2}", t.Minutes, t.Seconds);
}
//[PunRPC]
public static List<GameObject> MakePath(int team, int lane)
{
List<GameObject> newPath = new List<GameObject>();
int otherTeam = team == 0 ? 1 : 0;
foreach (GameObject go in Instance.Teams[team].LaneSpawnPoints[lane].Waypoints)
{
newPath.Add(go);
}
for (int i = Instance.Teams[otherTeam].LaneSpawnPoints[lane].Waypoints.Count - 1; i >= 0; i--)
{
newPath.Add(Instance.Teams[otherTeam].LaneSpawnPoints[lane].Waypoints[i]);
}
return newPath;
}
}
the wave manager summon minions through this code line
[PunRPC]
void SpawnUnit(string prefab, int spawnLoc)
{
GameObject go = PhotonNetwork.Instantiate(Path.Combine("Minions", prefab),
SpawnPoints[spawnLoc].transform.position,
Quaternion.identity, 1);
go.transform.SetParent(Minions);
Tropa tropa = go.GetComponent<Tropa>();
tropa.GetComponent<PhotonView>().RPC("ChangePath", RpcTarget.AllBufferedViaServer, InGameManager.MakePath(TeamID, spawnLoc));
//tropa.target = tropa.Path[0].transform;
}
that calls a RPC to Tropa for changed this Path
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.AI;
public class Tropa : MonoBehaviour
{
public int PathCount = 0;
public float dist;
public List<GameObject> Path;
}
[PunRPC]
public void UpdatePath(List<GameObject> path)
{
Path = path;
}
where did I go wrong? Thanks for your help, i love you<3
Comment
Your answer
Follow this Question
Related Questions
[PUN] How do I use RPCs the right way? 1 Answer
Game Objects in Multiple Lists? 0 Answers
Multiplayer car racing wining senior 0 Answers
PhotonPun RPC functions Doesnt Work For Both the Players 0 Answers
Shake GameObject in 0 Answers