Sprite Renderer is freaking out in the player, but not in the “Game” tab in the editor. My potions and the enemies are disappearing in the player, but they’re fine in the editor.
Here is the code for the monsters’ AI:
// What do the monsters do in certain situations?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
[RequireComponent(typeof(Rigidbody2D))]
public class MonsterAI : MonoBehaviour {
#region Public Variables
// What variables need to be accessed by other scripts?
[HideInInspector]
public GameObject hitByLast; // Who hit us last?
public Player[] players; // Who are the players?
public float health; // Where is our health right now?
public int damp = 5; // we can change the slerp velocity here
#endregion
#region Serialized Variables
// What variables can the Unity inspector see, but other scripts can't change?
[SerializeField]
float maxHealth; // Where does our health start?
[SerializeField]
float powerWorth; // How much power do we give to the player if they kill us?
[SerializeField]
int damage; // How much damage do we deal?
[SerializeField]
int waitTimeBeforeRecover; // How long do we take to recover?
[SerializeField]
float speed = 0.06f; // How fast are we?
[SerializeField]
bool destroyOnImpact; // Do we blow up when we damage the player(s)?
[SerializeField]
float sight; // How far can see?
[SerializeField]
Slider healthSlider; // What's our health bar?
#endregion
#region Private Variables
// What variables do we keep to ourselves?
bool recovering; // Are we recovering?
Animator anim; // What animates our monster?
#endregion
#region General
// What functions do we need that don't do anything specific?
private void Start()
{ // What do we do at the very first frame?
anim = GetComponent<Animator>(); // Obtain the animation controller.
}
void Update () {
// What do we do every frame?
for (int i = 0; i < players.Length; i++)
{ // Loop for how many players we have.
Vector3 diff = players[i].transform.position - transform.position;
diff.Normalize();
float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90);
// <pythagoreanTheorum>
float a = transform.position.x - players [i].transform.position.x; // How far away on the x axis are we from the player?
float b = transform.position.y - players [i].transform.position.y; // How far away on the y axis are we from the player?
float c = Mathf.Abs(Mathf.Sqrt(a*a+b*b)); // How far away are we from the player?
// </pythagoreanTheorum>
if (c <= sight && !recovering)
{ // Can we see the player? Are we recovering?
transform.position = Vector2.MoveTowards(transform.position,players[i].transform.position,speed*Time.deltaTime); // If we can see the player, get him!
anim.SetBool("walking", true); // Animate walking cycle.
} else {
anim.SetBool("walking", false); // Stop animating walking cycle.
}
}
if (health <= 0 && health != maxHealth) { // Are we out of health?
if (hitByLast.GetComponent<Player>() != null) // Were we killed by a player?
hitByLast.GetComponent<Player>().power += powerWorth; // If so, give them a reward.
Destroy (gameObject); // If we are out of health, we want to die!
}
healthSlider.maxValue = maxHealth; // Set the health bar's maximum.
healthSlider.value = health; // Set the health bar's value.
}
#endregion
#region Collision and Damage
// What are we colliding with if we are colliding? When do we do damage?
void OnCollisionStay2D(Collision2D other){ // Collision check: Are we colliding with the player object?
if (other.collider.GetComponent<Player>() != null && !recovering) { // Is what we're colliding with the player? Are we recovering?
Player plyr = other.collider.GetComponent<Player> (); // If it's the player and we're NOT recovering, get the player component.
plyr.TakeDamage (damage); // Deal damage to the player
plyr.hitByLast = gameObject; // Set the last one to hit the player for kill dialog.
if (destroyOnImpact) { // Do we blow up?
Destroy (gameObject); // If so, kill ourselves.
} else {
recovering = true; // If not, make sure we don't do more damage.
StartCoroutine ("Recover"); // Start to recover from an attack.
}
}
}
IEnumerator Recover(){ // What do we do to recover?
yield return new WaitForSeconds (waitTimeBeforeRecover); // Wait for a little bit.
recovering = false; // We're done recovering, so tell the code that we're not recovering anymore.
}
#endregion
}
Here is the code for the potions:
Health:
using UnityEngine;
using System.Collections;
public class HealthPotion : MonoBehaviour {
#region Variables
public int healthAdd = 4;
public int waitTime = 60;
#endregion
#region Functions
void OnTriggerEnter2D(Collider2D other){
if (other.GetComponent<Player>() != null) {
Player plyr = other.GetComponent<Player> ();
plyr.HealDamage (healthAdd);
gameObject.transform.position = new Vector3 (Mathf.FloorToInt(Random.Range(-8,8)),Mathf.FloorToInt(Random.Range(-4,4)));
StartCoroutine ("Respawn");
}
}
void Start(){
StartCoroutine ("Respawn");
}
IEnumerator Respawn(){
yield return new WaitForSeconds (waitTime);
gameObject.transform.position = new Vector3 (Mathf.FloorToInt(Random.Range(-8,8)),Mathf.FloorToInt(Random.Range(-4,4)));
}
#endregion
}
Speed:
using UnityEngine;
using System.Collections;
public class SpeedPotion : MonoBehaviour {
#region Variables
public int speedMultiplier = 2;
public int waitTime = 60;
#endregion
#region Functions
void OnTriggerEnter2D(Collider2D other){
if (other.GetComponent<Player>() != null) {
Player plyr = other.GetComponent<Player> ();
plyr.MultiplySpeed (speedMultiplier);
gameObject.transform.position = new Vector3 (Mathf.FloorToInt(Random.Range(-8,8)),Mathf.FloorToInt(Random.Range(-4,4)));
StartCoroutine ("Respawn");
}
}
void Start(){
StartCoroutine ("Respawn");
}
IEnumerator Respawn(){
yield return new WaitForSeconds (waitTime);
gameObject.transform.position = new Vector3 (Mathf.FloorToInt(Random.Range(-8,8)),Mathf.FloorToInt(Random.Range(-4,4)));
}
#endregion
}