public void Possess(GameObject possObj, GameObject _player) { Destroy(possObj.GetComponent <Enemy>()); // Remove Enemy script from possessed object possObj.GetComponent <SphereCollider>().radius = 1; // Shrink it's collider radius possessedAnim = possObj.GetComponent <Animator>(); // Get possessed object's animator possObj.AddComponent <PlayerController>(); // Add PlayerController script //mainCamera.transform.SetParent(possObj.transform); // Change main camera's parent to possessed object //mainCamera.transform.position = possObj.transform.position + camOffset; possObj.tag = "Player"; // Change possessed object's tag to "Player" Destroy(_player.GetComponent <PlayerController>()); // Destroy player's (or previous possessed object's) PlayerController script _player.tag = "wasPossessed"; //_player.GetComponent<Possessed>().ResetStats(); Destroy(_player.GetComponent <Possessed>()); // Destroy previous possessed object's Possessed script //Debug.Log(player.health + " " + possObj.GetComponent<Enemy>().health); possObj.AddComponent <Possessed>(); // Add Possessed script to possessed object //possObj.GetComponent<Possessed>().health = player.health + possObj.GetComponent<Enemy>().maxHealth; possObj.GetComponent <Possessed>().AddStats(possObj.GetComponent <Enemy>().maxHealth, possObj.GetComponent <Enemy>().dmg); // Add player's and possessed object's stats for (int i = 0; i < enemies.Length; i++) // Reset enemies array { enemies[i] = null; } enemies = GameObject.FindGameObjectsWithTag("Enemy"); // Get all enemies Debug.Log("POSESSING"); possessed = possObj.GetComponent <Possessed>(); // Get Possessed script StartCoroutine(Delete(_player)); // Destroy previous possessed object }
// Update is called once per frame void Update() { if (GameManager.Instance.possessed != null) { possesed = GameManager.Instance.possessed; string[] values = possesed.gameObject.name.Split('('); Name.text = values[0]; Health.text = possesed.health.ToString(); Dmg.text = possesed.dmg.ToString(); } else { player = GameManager.Instance.player; Name.text = "Ghost"; Health.text = player.health.ToString(); Dmg.text = player.dmg.ToString(); } }