Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by CCGnomes · Oct 02, 2016 at 11:09 PM · scorescore systempoints

How do I add extra points for coming close to obstacle but NOT for coming close and colliding?

Here is what I have to check to see if object is close:

 void CloseCall()
     {
         
         GameObject[] x= GameObject.FindGameObjectsWithTag("xx");
         foreach(GameObject target in x) {
             float distance = Vector3.Distance(target.transform.position, transform.position);
             if(distance < 10 ) {
                 
                 //if in a certain distance add extra points for near missing obstacles with tag
                 
             }
                 
         }
Comment
Add comment · Show 5
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 TBruce · Oct 02, 2016 at 11:42 PM 0
Share

FindGameObjectsWithTag() is slow. And doing that every frame is even slower.

Ins$$anonymous$$d of that why not add an appropriate collider to each GameObject, but resize the collider so that it is larger than the object itself (to the appropriate size). $$anonymous$$ark the "Is Trigger" checkbox on the collider. Then add a script with an OnTriggerEnter(), or OnTriggerEnter2D(), something like this (just an example)

 using UnityEngine;
 using System.Collections;
 
 public class EnemyScript : $$anonymous$$onoBehaviour
 {
     private PlayerScript playerScript = null;
 
     // Use this for initialization
     void Start ()
     {
         if ((GameObject.FindWithTag("Player") != null) && (GameObject.FindWithTag("Player").Getcomponent<PlayerScript>() != null))
         {
             playerScript = GameObject.FindWithTag("Player").Getcomponent<PlayerScript>();
         }
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.CompareTag("Player"))
         {
             if (playerScript != null)
             {
                 playerScript.points++;
             }
         }
     }
 }

If you already have a collider on the object then add a child object (call it Hitbox), add the collider to the HitBox game object. Set the Is Trigger checkbox on the collider. $$anonymous$$ake the collider the appropriate size.

Note: You also need to add a Rigidbody (or Rigidbody2D) to one of the objects (preferably the player). And you may want to disable gravity (depending on your game type).

avatar image TBruce TBruce · Oct 03, 2016 at 01:20 AM 0
Share

@CCGnomes Did you find this answer helpful?

avatar image CCGnomes TBruce · Oct 03, 2016 at 04:11 AM 0
Share

Won't the player still get the points if they collide with the object because they are going through the trigger ?

avatar image TBruce CCGnomes · Oct 03, 2016 at 04:56 AM 0
Share

Depends what you are looking for and how big you make the collider.

Imagine a square and the collider is twice the size of the square. The trigger will occur when the player reaches the edge of the collider, not when it collides with the edge of the square.

And if you want you can add a flag that is set when the player enters and exits the collider, loke so

 using UnityEngine;
 using System.Collections;
 
 public class EnemyScript : $$anonymous$$onoBehaviour
 {
     private PlayerScript playerScript = null;
     private bool triggered = false;
 
     // Use this for initialization
     void Start ()
     {
         if ((GameObject.FindWithTag("Player") != null) && (GameObject.FindWithTag("Player").Getcomponent<PlayerScript>() != null))
         {
             playerScript = GameObject.FindWithTag("Player").Getcomponent<PlayerScript>();
         }
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.CompareTag("Player") && (!triggered))
         {
             triggered = true;
             if (playerScript != null)
             {
                 playerScript.points++;
             }
         }
     }
 
     void OnTriggerExit(Collider other)
     {
         if (other.CompareTag("Player") && (triggered))
         {
             triggered = false;
         }
     }
 }
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
-1
Best Answer

Answer by CCGnomes · Oct 03, 2016 at 07:07 PM

I think i figured it out. My solution was kind of what you said with the triggers but check the players health as soon as it hits trigger and on trigger exit have it add points ONLY if the player has the same health. Or for example if I didn't have health. Use a count system that checks to see if the number that it is before it hits the trigger is the same after it exits trigger and have it increment on collisions.

Finally had a brain blast today. Hopefully this can help someone in the future.

Comment
Add comment · Share
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

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

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

Related Questions

Help With Score C# Error CS0131 1 Answer

Scoring Points with UI Problem! 0 Answers

reload game 1 Answer

UI printing in multiple sections.(not supposed to) c#(Solved) 1 Answer

Spawn Object on Set Score 0 Answers


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