• 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
Question by gimoj · Jun 09, 2014 at 11:18 AM · collisiontriggerscoredetection

Adds too many points to the score?

Can anybody help me with my scoring? I have it so that the player gets a point whenever it destroys an enemy but what happens is that sometimes it adds too many. It sometimes works as it should and adds 1 point to the score but it occasionally adds 3,4 or sometimes even 10. I don't know if it's also related but I tried making it so that an animation plays once when it hits the front check but the animation always plays twice.

How can I make it that it only adds one whenever it destroys an enemy and/or only one animation plays?

Thanks, I this this is all basic but I've tried researching this all over the net but nothing works.

 void Awake()
 {
     frontCheck = transform.Find("frontCheck").transform;
     beak = transform.Find("beak").transform;
 }
 
 void Start () 
 {
     animator = GetComponent<Animator>();
 }

 void Update ()
 {
     walking = true;
     
 }

 void FixedUpdate ()
 {    
             GroundCheck ();

     if (grounded == false || dragged == true) 
     {
         float h = Input.GetAxis ("Mouse X");
         
         if (h > 0 && !facingRight)
             Flip ();
         else if (h < 0 && facingRight)
             Flip ();
     }

             if (grounded == true) 
                 {
                     if (walking == true) {
                             walker ();
                     }

                     Collider2D[] frontHits = Physics2D.OverlapPointAll (frontCheck.position);
     
                     foreach (Collider2D c in frontHits) {

                             if (c.tag == "DirectionTrigger") {

                                     Flip ();
                                     break;
                             }

                             if (c.tag == "Enemy") {

                                     animator.SetTrigger ("dash");
                                     walking = false;

                                     break;
                             }
     
                     }

                 Collider2D[] beakhits = Physics2D.OverlapPointAll (beak.position);
                     
                 foreach (Collider2D b in beakhits) {
                         
                         if (b.tag == "Enemy") 
                         {
                             if (b.gameObject.GetComponent<Waspbot> ().BodyHit == true)
                             {
                                 b.gameObject.GetComponent<Waspbot> ().HitReceived (transform.position);
                                 CollateralDamage ();

                             }
                         }
                         
                     }

                 }

         if(HP <= 0 && !dead)
             {
                 Death ();
                 StartCoroutine(delayAnim());
             }
     }


 public void walker()
 {
     rigidbody2D.velocity = new Vector3 (transform.localScale.x * WalkSpeed, rigidbody2D.velocity.y, transform.localScale.y * WalkSpeed);
     
 }

 void GroundCheck()
 {
     Debug.Log ("grounded");
     Debug.DrawLine (sightStart.position, sightEnd.position, Color.blue);
     grounded = Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer("HighGround"));
 }
 
 public void Flip()
 {
     // Switch the way the player is labelled as facing.
     facingRight = !facingRight;

     // Multiply the x component of localScale by -1.
     Vector3 beakbotScale = transform.localScale;
     beakbotScale.x *= -1;
     transform.localScale = beakbotScale;
 }

 public void Hurt()
 {
     HP--;
     Debug.Log (HP);
 }
 
 void Death()
 {
     StartCoroutine(Explosion());
 }

 public IEnumerator delayAnim()
 {
     yield return new WaitForSeconds(0.01f);
     animator.SetTrigger ("explosion");
 }
 
 public IEnumerator Explosion()
 {
     if (HP <= 0) 
     {
         yield return new WaitForSeconds(0.6f);
         Destroy (gameObject);
         GameObject go = GameObject.FindGameObjectWithTag("Botton");
         Bottons sb = (Bottons)go.GetComponent(typeof(Bottons));
         sb.bbotcount -= 1;
     }
 }

 void OnMouseDown()
 {
     dragged = true;
     screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
     
     offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
     
 }
 
 void OnMouseDrag()
 {
     Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
     
     Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) , offset;
     transform.position = curPosition;
     
 }

 void OnMouseUp()
 {
     dragged = false;
 }

 public void CollateralDamage ()
 {
     HP -= 1;
 }
 

 void OnTriggerEnter2D (Collider2D bbcol){
     
     // If it hits an enemy...
     if (bbcol.tag == "Enemy") {
         if (bbcol.gameObject.GetComponent<Waspbot> ().BodyHit == true)
         {
         bbcol.gameObject.GetComponent<Waspbot> ().RemoveHp ();
         HP -= 2;
         }
     }
 }
Comment

People who like this

0 Show 3
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 Jeff-Kesselman · Jun 09, 2014 at 11:42 AM 0
Share

Without seeing your code it is impossible to tell what you are doing wrong.

Please post it, properly formatted in a code block.

avatar image MrFijiWiji · Jun 09, 2014 at 12:00 PM 0
Share

A few more details might make it easier for somebody to help you. For instance, are you using 2D or 3D? OnCollision or OnTrigger?

avatar image gimoj · Jun 09, 2014 at 12:03 PM 0
Share

Ok sorry, I edited my post and put in my code.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Collision problem when spawning in the collider 1 Answer

How to implement ECS trigger collisions? (Unity.Physics 0.50) 1 Answer

Best practice for OnTriggerEnter detection 1 Answer

Detecting Trigger Collision for Mecanim Animator 0 Answers

I need help with collision detection event 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