OnCollisionStay not working when game is built
Hi, I was wondering why my collisions aren't working properly, specifically on Void OncollisionStay. I've also tried to use OnCollisionEnter getting no result when I build the game as counterpart when I test the game in the editor everything works really well. to give some context, this script is for a bullet which it will chase to the enemy "obj" unit Oncollisionstay detect him and then, calling the function "vida_ enemigo()" to substract some health "enem.vida". I would be so grateful if anyone could help me pls. :( here's my code:
public int damage;//modificar en inspector
public GameObject obj;//no modificar
public float velocidad;//modificar en inspector
Rigidbody rgb;//no modificar
public bool inicializar = false;//no modificar
void Start()
{
rgb = gameObject.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (obj && inicializar)
{
transform.eulerAngles = new Vector3(0, angulo_a_rotar(obj) - 180, 0);
rgb.transform.Translate(Vector3.forward * Time.deltaTime * velocidad);
}
if( !obj && inicializar)
{
Destroy(gameObject);
}
/*if(obj && checkear_distancia(0.3f, obj))
{
vida_enemigo(obj);
}*/
}
private void OnCollisionStay(Collision collision)
{
if (obj == collision.gameObject)
{
vida_enemigo(collision.gameObject);
}
}
void vida_enemigo(GameObject enemigo)
{
vida_enemigo enem = enemigo.GetComponent<vida_enemigo>();
enem.vida -= damage;
DestroyImmediate(gameObject);
}
float angulo_a_rotar(GameObject obj)
{
float dist = 0;
float eje_x = obj.transform.position.x - transform.position.x;
float eje_z = obj.transform.position.z - transform.position.z;
dist = Mathf.Sqrt(eje_x * eje_x + eje_z * eje_z);// dist de esta torreta hasta el enemigo
float dist_2 = 0;
float eje_x_2 = obj.transform.position.x - transform.position.x;
float eje_z_2 = obj.transform.position.z - transform.position.z + dist;
dist_2 = Mathf.Sqrt(eje_x_2 * eje_x_2 + eje_z_2 * eje_z_2);// dist del (torretax, torretaz + dist) hasta el enemigo
float dist_3 = dist;// dist de la torreta hasta (torretax, torretaz + dist)
float angulo = (180 / Mathf.PI) * Mathf.Acos(-1 * (dist_2 * dist_2) / (2 * dist_3 * dist) + (dist_3) / (2 * dist) + (dist) / (2 * dist_3));
if (obj.transform.position.x > transform.position.x)
{
angulo *= -1;
}
return angulo;
}
bool checkear_distancia(float distancia, GameObject obj)
{
float dist = 0;
float eje_x = obj.transform.position.x - transform.position.x;
float eje_z = obj.transform.position.z - transform.position.z;
dist = Mathf.Sqrt(eje_x * eje_x + eje_z * eje_z);
if (dist <= distancia)
{
return true;
}
else
{
return false;
}
}
}
I figured out!, it was an issue that i committed with some tags of an GameObject. Nothing related to this issue :').
Your answer
Follow this Question
Related Questions
Can't see GameObjects In Scene View 2 Answers
when i try to build for window platform 82 and 64 both ;( 0 Answers
[SOLVED] Constructors with unexpected default values 1 Answer
Changing material dose not change the appearance of the game object 0 Answers
Unity IAP not working for android need google public key 0 Answers