Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Server game object not syncing position after if statement

$
0
0
I am currently working on an enemy in my video game. It follows the closest player that is in range and then stops when in a certain distance from the player. Once that happens, it damages the player. I want to make it so the enemy would check for other players in range once the current player dies. Currently, the enemy moves to the position of the closest alive player once a player dies, but only on one of the clients. I want to know why the position isn't syncing after a player dies. Here is a video of what happens. Don't mind the weird images, they're just placeholders. [https://youtu.be/F_6rAqVn32k][1] // Transfer enemy position data to server enemyPosition = gameObject.transform; // For each player online foreach (GameObject player in players) { try { // If dictionary already contains player, skip if (distances.ContainsKey(player)) { } // If dictionary doesn't contain player, add to dictionary else { // Player key, distance between player and enemy value distances.Add(player, Vector3.Distance(player.transform.position, transform.position)); } // If the amount of entries in dictionary is equal to player count if (distances.Count == playerCount) { // Sort dictionary least to greatest var sortedDict = from ele in distances orderby ele.Value ascending select ele; // Get first entry in dictionary (will be closest distance) var first = sortedDict.First(); // Sets closest player closestPlayer = first.Key; // Finds distance between closest player and enemy position distanceBetween = Vector3.Distance(closestPlayer.transform.position, transform.position); // If distance between closest player and enemy position is less or equal to follow distance if (distanceBetween <= followDistance) { // Sets agent to not stopped agent.isStopped = false; // Sets agent destination to closest player position agent.SetDestination(closestPlayer.transform.position); // Clears distance dictionary to reuse // Break loop break; } } } // If player disconnects, it always throws MissingReferenceException catch (MissingReferenceException) { // Sort dictionary least to greatest var sortedDict = from ele in distances orderby ele.Value ascending select ele; // Finds second closest player var second = sortedDict.ElementAt(0); // Sets closest player closestPlayer = second.Key; // Finds distance between enemy position and closest player distanceBetween = Vector3.Distance(closestPlayer.transform.position, transform.position); // If distance between enemy position and closest player is less or equal to follow distance if (distanceBetween <= followDistance) { // Sets agent to not stopped agent.isStopped = false; // Sets agent destination to closest player position agent.SetDestination(closestPlayer.transform.position); // Clears distance list to reuse // Breaks loop break; } } } // Checks if closest player is dead dead = closestPlayer.GetComponent().isDead; // If distance between closest player and enemy is within stopping distance if (distanceBetween <= stoppingDistance) { // If player isn't dead if (dead == false) { // Stops NavMeshAgent agent.isStopped = true; } // If time between attacks is less or equal to zero if (timeBtwShots <= 0) { // Deals damage to player closestPlayer.GetComponent().TakeDamage(100); // Resets timer timeBtwShots = startTimeBtwShots; } else { timeBtwShots -= Time.deltaTime; } } // Closest player is dead if (dead) { // Converts distance dictionary to list distanceList = distances.Keys.ToList(); // Removes first list entry distanceList.Remove(closestPlayer); // Get first entry in dictionary (will be closest distance) var first = distanceList.First(); // Sets closest player closestPlayer = first; // Resumes agent agent.isStopped = false; // Sets agent destination to next closest player agent.SetDestination(closestPlayer.transform.position); } // Clears dictionary distances.Clear(); distanceList.Clear(); } [1]: https://youtu.be/F_6rAqVn32k

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>