Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jeffreywarf · Jun 05, 2013 at 11:28 AM · collisiontime3dtext

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jeffreywarf · Jun 05, 2013 at 06:30 AM 0
Share

Found the answer. Turns out I had this script attached to two objects so that's why it was giving me weird bugs.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Time release Collectable 1 Answer

How can I make it so that inputs only function once per second? 2 Answers

How to detect collision when Time.timeScale == 0? 2 Answers

Comparing time. Missing something? 2 Answers

Parenting after X time with collision, I dont get it! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges