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 Greek_Soldier · Jul 03, 2014 at 03:51 AM · c#collisiontriggersdetecting

Detecting collisions

I've been trying to get my enemies to detect my player immediately using OnTriggerEnter, OnTriggerExit, and OnTriggerStay but I haven't gotten any of them to work. Does anyone know a void/method/function that could hep me here in c#?

edit: I recently edited my code and now there are two codes that coordinate the score together. they go like this:

using UnityEngine; using System.Collections;

public class GameOver : MonoBehaviour {

 void OnTriggerEnter(Collider other){
 
     SendMessageUpwards("HasTriggered",gameObject);
     
 }
 
 
 

}

using UnityEngine; using System.Collections;

public class GameManager : MonoBehaviour {

 public bool isCounting = false;
 public float time = 0;
 public bool HT = false;
 
 private void HasTriggered(GameObject tempGO){
     HT = true;
 }

 
 
 
 
 
  public void Update()
 {
     if (HT == false) {
         isCounting = true;
         Invoke ( "tick", 0f );
     }
 }
 
 private void tick() {
     if(HT == false) {
         time += 1;
     } else {
         isCounting = false;
     }
 }
 
 void OnGUI(){
     
     GUI.Label (new Rect(10,10,100,50),"score:" + time);
     
     
     
     if(HT){
         GUI.Label(new Rect(Screen.width / 2 -50, Screen.height / 2 - 75,200,100),"YOUR SCORE WAS " + time);
     
         if(GUI.Button(new Rect(Screen.width / 2 -50, Screen.height / 2 - 25,100,50),"RESTART?")){
           Application.LoadLevel(0);
         }
     }
 }
 

}

again, thank you for helping me.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by BsseeJ · Jul 03, 2014 at 06:18 AM

Hello Greek_Soldier.

In Monobehavior there is a message method called OnTriggerEnter and the rest of the Triggers/colliders.

Here is an example of one: http://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html

I'll try to help and explain it to you because I'm not sure how much you know based off your post.

     void OnTriggerEnter(Collider other) {
         Destroy(other.gameObject);
     }


Firstly you have a return type of void, then you must have the "OnTriggerEnter" name to call upon this Monobehavior method. In the parameters of the method you have to have the class Collider as the first arguement, and then name it. This is whatever the game object which you attached this script to collision was, so for example if a bullet hits and enemy and you have this attached to the bullet the enemy's collider component is "other". In their example they name it other, it could be anything but that is what it is, the other collider that hit the game object's collider.

Then if you want to do something to the object that hits the collider you must start with the (in this case) "other" keyword so that the script knows that you want to effect the object colliding with your game object you have this script attached to. Then you use the dot syntax to call it's gameObject component, this is just the component that resembles the entire game object. If you do not do "other.gameObject" the class will think you are only accessing the collision component instead of the entire gameObject.

I hope this helped! There are also videos on the unity Learn section.

PS. Make sure both the colliders being affected are set to triggers in the inspector. PSS. If this is 2D you need to use a 2D collider method not the normal one.

Comment
Add comment · Show 1 · 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
avatar image Greek_Soldier · Jul 06, 2014 at 07:17 PM 0
Share

thanks for trying to help, but actually I'm using that method in my current code(I'll edit my post and put it up) and my main problem is that someone could just stand still and completely negate the effects of OnTriggerEnter.

avatar image
0

Answer by king_ · Jul 03, 2014 at 04:44 AM

try OnCollisionEnter, OnCollisionExit, OnCollisionStay,

Comment
Add comment · Show 1 · 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
avatar image Greek_Soldier · Jul 06, 2014 at 07:20 PM 0
Share

I've tryed all of those all ready and my problem still persists.

avatar image
0

Answer by chris3199 · Jul 03, 2014 at 06:16 AM

 // Attach this code to your Enemies code.
 
 public void OnTriggerEnter(Collider other)
 {
    if(other.Name == "Player")
    {
       //Once the player has triggered the collider
       //we can do stuff like,
  
       Destroy(other);
       //The line above will destroy the player once it has entered the collider. 
    }
 }
Comment
Add comment · Show 1 · 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
avatar image Greek_Soldier · Jul 06, 2014 at 07:26 PM 0
Share

actually my current code is very similar to this but while it eventually does work if the character is moving, if the player stands still he can completely bypass the system.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

How do I make a car in my driving scene trigger the trotting of a deer (on collision with a box collider around another object) and then make the deer die when my car collides with the deer?, 0 Answers

How do I ignore trigger objects for collision? 0 Answers

Trigger Collision Moves When Object Enters 1 Answer

Specific GameObject Collision Issue 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