Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

collider and saving. enemies stop detection?

Discussion in 'Multiplayer' started by Resilo, Dec 24, 2017.

  1. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    So i have a game that is client server based. i have a save crystal that is used via a collider on the players character colliding with the crystal

    upon starting the game it all works perfectly. however as soon as an enemy spawns the players collider on the use object can no longer see the save crystal at all.
    yes the save crystal object is a registered spawn with the network manager

    heres the spawn script

    Code (CSharp):
    1. using UnityEngine.Networking;
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4. using System.Collections;
    5.  
    6. public class Enemyspawn : NetworkBehaviour
    7. {
    8.  
    9.     public GameObject objectToSpawn;
    10.     public float timer;
    11.     public bool canspawn;
    12.     [SyncVar]
    13.     public float weight;
    14.     [SyncVar]
    15.     public Vector3 npcsize;
    16.     [SyncVar]
    17.     public float size;
    18.     public GameObject[] enemiesparty;
    19.     bool spawnthis;
    20.     public int enemies;
    21.         public int enemiesmax;
    22.  
    23.     public List<GameObject> players = new List<GameObject>();
    24.     public GameObject spawnbutton;
    25.     public GameObject spawnpoint;
    26.     bool starter;
    27.  
    28.  
    29.     public void Start()
    30.     {
    31.         if (!isServer)
    32.         {
    33.             Destroy(spawnbutton);
    34.         }
    35.  
    36.     }
    37.  
    38.     IEnumerator spanwer()
    39.     {
    40.         canspawn = true;
    41.         yield return new WaitForSecondsRealtime(timer);
    42.  
    43.         SpawnEnemy();
    44.         canspawn = false;
    45.  
    46.     }
    47.  
    48.  
    49.  
    50.     void spawn()
    51.     {
    52.  
    53.     if (enemies < enemiesmax && enemies != enemiesmax && canspawn == false)
    54.         {
    55.  
    56.                 StartCoroutine(spanwer());
    57.          
    58.         }
    59.     }
    60.  
    61.  
    62.     public void LateUpdate()
    63.     {
    64.         if (!isServer)
    65.         {
    66.             return;
    67.         }
    68.         spawn();
    69.     }
    70.  
    71.     public  void SpawnEnemy()
    72.     {
    73.         if(!isServer)
    74.         {
    75.             return;
    76.         }
    77.  
    78.  
    79.         GameObject enemy = Instantiate(objectToSpawn, spawnpoint.transform.position, spawnpoint.transform.rotation);
    80.  
    81.         weight = Random.Range(150, 190);
    82.         enemy.GetComponent<Charactersize>().weight = weight;
    83.         enemy.GetComponent<myspawnobject>().spawner = gameObject;
    84.         players.Add(enemy);
    85.             foreach (GameObject partymembers in players)
    86.             {
    87.             if(!partymembers.GetComponent<Stats>().party.Contains(enemy))
    88.  
    89.                 partymembers.GetComponent<Stats>().party.Add(enemy);
    90.             }
    91.      
    92.  
    93.         NetworkServer.Spawn(enemy);
    94.         enemies += 1;
    95.     }
    96. }



    script for using an object


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class useobject : MonoBehaviour {
    6.     public GameObject player;
    7.     public GameObject objectinuse;
    8.     public bool canuse;
    9.     public int xp;
    10.     public float hp;
    11.     public float maxhp;
    12.     public int level;
    13.     public int unarmedlevel;
    14.     public int unarmedxp;
    15.     public int unarmedxptonextlevel;
    16.     void OnTriggerEnter(Collider other)
    17.  
    18.     {
    19.         if (other.GetComponent<beingused>() != null)
    20.             {
    21.             player.GetComponent<Stats>().UIuseobject.SetActive(true);
    22.             objectinuse = other.gameObject;
    23.             canuse = true;
    24.         }
    25.     }
    26.  
    27.  
    28.  
    29.  
    30.     void OnTriggerExit(Collider other)
    31.  
    32.     {
    33.         if (other.GetComponent<beingused>() != null)
    34.         {
    35.  
    36.             other.GetComponent<beingused>().useobject = false;
    37.             player.GetComponent<Stats>().UIuseobject.SetActive(false);
    38.             canuse = false;
    39.             objectinuse = null;
    40.         }
    41.     }
    42.  
    43.  
    44.     void usingobjecty()
    45.     {
    46.         if (canuse == true)
    47.         {
    48.             if (Input.GetKeyDown(KeyCode.E))
    49.             {
    50.                 player.GetComponent<Stats>().playeraudio.clip = player.GetComponent<Stats>().selectobject;
    51.                 player.GetComponent<Stats>().playeraudio.Play();
    52.                 if(objectinuse.GetComponent<savecrystal>() != null)
    53.                     {
    54.                     objectinuse.GetComponent<savecrystal>().level = player.GetComponent<Stats>().level;
    55.                     objectinuse.GetComponent<savecrystal>().xp = player.GetComponent<Stats>().xp;
    56.                     objectinuse.GetComponent<savecrystal>().hp = (int)player.GetComponent<Stats>().hp;
    57.                     objectinuse.GetComponent<savecrystal>().maxhp = (int)player.GetComponent<Stats>().maxhp;
    58.                     objectinuse.GetComponent<savecrystal>().username = player.GetComponent<list>().username;
    59.                     objectinuse.GetComponent<savecrystal>().unarmedlevel = (int)player.GetComponent<Skill>().unarmedlevel;
    60.                     objectinuse.GetComponent<savecrystal>().unarmedxp = (int)player.GetComponent<Skill>().unarmedxp;
    61.                     objectinuse.GetComponent<savecrystal>().unarmedxptonextlevel = (int)player.GetComponent<Skill>().unarmedxptonextlevel;
    62.  
    63.                 }
    64.                 objectinuse.GetComponent<beingused>().useobject = true;
    65.             }
    66.         }
    67.     }
    68.     // Use this for initialization
    69.     void Start () {
    70.      
    71.     }
    72.  
    73.     // Update is called once per frame
    74.     void Update () {
    75.         usingobjecty();
    76.  
    77.     }
    78. }
    79.  





    here is a screenshot of the crystal and how it is configured



    https://gyazo.com/433b2951ec0dc271366505909492355b


    the only thing missing in the screen shot is the network transform which it does have
    set to sync the rigid body and has a send rate of 0


    this is the use object configuration


    https://gyazo.com/70ee8aa4d3db8b7236b300a80ae898ed



    can provide any additional details just let me know any information you need
     
    Last edited: Dec 24, 2017