Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by nwott1 · Jan 13, 2021 at 01:09 AM · multiplayerenemymultiplayer-networkingenemy aimirror

Server game object not syncing position after if statement

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

 // 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<PlayerMovement>().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<PlayerMovement>().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<GameObject>();
             // 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();
     }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

202 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a game object detect if any of multiple game objects are in range 2 Answers

Character isn't moving in multiplayer with buttons. 0 Answers

How to handle raycast shooting in multiplayer (Mirror)? 0 Answers

Multiplayer Game Highlight Material 0 Answers

how to ignore collision on player object in unity multiplayer(2 player) using mirror or unet(depreciated) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges