- Home /
Collision Detection Issues
I have this code: #pragma strict
var colliding : boolean = false;
var the_player : GameObject;
var target_lookat : Transform;
var target_position : Vector3;
var enemy_health : int;
var enemy_health_max : int;
var enemy_level : int;
var enemy_damage : int;
var thetext : GameObject;
var spawned_text : GameObject;
var damage_text: TextMesh;
var text_spawned : boolean;
var rotation : Vector3;
var enemy_itself : GameObject;
var player_target : GameObject;
var player_position : Transform;
var location : Vector3;
var loot : GameObject[];
var rand_loot : int;
var rand_modifier : int;
var attack_timer : float = 1.1;
var attacking_enemy : int;
var xp_reward : double;
//Statuses
var stunned : boolean = false;
var poisoned : boolean = false;
var bleeding : boolean = false;
var target_shielded : boolean = false;
var has_evasion : boolean = false;
var evaded : boolean = false;
function Start () {
var target_lookat = GameObject.FindGameObjectWithTag("player");
the_player = GameObject.FindGameObjectWithTag("player");
var OtherScript: Experience_Calc = GetComponent(Experience_Calc);
//thetext = GameObject.FindGameObjectWithTag("damage");
enemy_level = the_player.GetComponent(Experience_Calc).level;
enemy_damage = enemy_level * 5;
enemy_health_max = enemy_level * 75;
enemy_health = enemy_health_max;
}
function GetRand(a : int, b : int){
rand_loot = Random.Range(a,b);
}
function Update () {
target_position = the_player.transform.position;
transform.LookAt(the_player.transform);
xp_reward = (enemy_level * 25) + (the_player.GetComponent(Experience_Calc).exp_needed / 100);
attack_timer = attack_timer - Time.deltaTime;
if (attack_timer <= 0.0)
{
attack_timer = 0.0;
}
if(colliding == true)
{
if(player_target.GetComponent(Experience_Calc).cur_health >= 0 && attack_timer <= 0.0)
{
player_target.GetComponent(Experience_Calc).cur_health -= enemy_damage;
attack_timer = 1.1;
}
else if(player_target.GetComponent(Experience_Calc).cur_health <= 0 && attack_timer <= 0.0)
{
player_target.GetComponent(Experience_Calc).cur_health = 0;
spawned_text = Instantiate(thetext,the_player.transform.position,Quaternion.identity);
text_spawned = true;
attack_timer = 1.1;
}
}
else if(colliding == false || attack_timer >= 0.0)
{
//damage_text.text = "";
//Debug.Log(attack_timer.ToString());
//PlayerPrefs.SetInt("InRange", 0);
}
if(attack_timer >= 1.1)
{
spawned_text = Instantiate(thetext,the_player.transform.position,Quaternion.identity);
text_spawned = true;
}
else if(attack_timer > 0.0)
{
text_spawned = false;
}
if(text_spawned == true)
{
damage_text = spawned_text.GetComponent(TextMesh);
damage_text.fontSize = Screen.height / 10;
damage_text.text = "-" + enemy_damage.ToString();
}
enemy_level = PlayerPrefs.GetInt("PlayersLevel");
if(enemy_health <= 0)
{
GetRand(0,4);
Instantiate(loot[rand_loot],enemy_itself.transform.position,Quaternion.identity);
the_player.GetComponent(Experience_Calc).current_xp += xp_reward;
Destroy(enemy_itself);
}
}
function OnTriggerEnter (collision : Collider) {
if(collision.tag == "player")
{
colliding = true;
player_target = collision.gameObject;
Debug.Log("In Range");
}
else if(collision.tag != "player")
{
colliding = false;
}
Debug.Log("Attacking");
}
function OnTriggerExit(collision : Collider){
if(collision.tag == "player")
{
colliding = false;
}
}
Now what my problem is is that in one scene, when I enter the attack range (a sphere in front of a prefab model) one of five things happen (I don't know which factors cause which).
1) I enter the attack range of the enemy, it hits once, the 3D text spawns above the player and it does the correct damage and it attacks normally (dealing damage once per second).
2) The above happens, but the attack is initiated only once, so all that happens only once.
3) While in the attack range and attacking is happening, it does double the normal damage even though it clearly says "-5" (or whatever the current damage is) when it appears over the player (the 5 is from a variable if you read the code). And also, the text will only spawn once, while damage continues to be double of normal.
4) The attack damage is double of normal yet the text spawns normally.
5) Only for the very first time an enemy tries to attack the player does the text and attack only happen once and then the player receives no damage unless they exit the attack range and go back into it. Then it is forever normal for the rest of the game.
Honestly, there are so many different results it's impossible to list them all, why would any of these things happen even if I never change the code?
Found the answer. Turns out I had this script attached to two objects so that's why it was giving me weird bugs.