Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 SylvesterAHansen · Jul 16, 2015 at 12:33 PM · unity5fire

OnTriggerEnter2D fires multiple times on play

Hi there

I have an issue, which I do not understand at all and I can find no one who seems to have the same problem. I have a game created in Unity using the 2d components with c# as the main scripting language.

The set up is as follows. I have a playable character, which gets a vector2 on Input.GetMouseButtonDown(0), that it then moves too and I have a camera controller which gets its vector3 from that same function. To detach the camera from the character and make an end of the scene, I have created two sets of triggers at either end of the scene, which will detach the controller from the camera. This way I have an end to the scene and the character is unable to move any further.

My problem is this: every so often, when I hit play, the trigger will fire non stop. This is not something that happens every time I hit play, but just once in a while and it is driving me up the wall.

this is my code:

on the player:

 public class charCtrl : MonoBehaviour {
 
     public float speed;
     
     Vector2 target;
     
     public cameraCtrl CamCtrl;
     
     void Start () {
         
         target = transform.position;
 
         CamCtrl = FindObjectOfType <cameraCtrl>();
     }
     
     void Update () {
 
         if (Input.GetMouseButtonDown(0) && !stopHer) {
             target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             target.y = -0f;
         }
 
         CamCtrl.getMeThere = target;
         CamCtrl.camSpeed = speed;
 
         transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
         
     }
 
     public void stopTheChar (){
 
         target = transform.position;
 
     }
 }
 

this is the border:

 public class sceneBorder : MonoBehaviour {
 
     public cameraCtrl mainCamera;
 
     bool camMotion;
 
     public bool inOut;
 
     // Use this for initialization
     void Start () {
     
         mainCamera = FindObjectOfType<cameraCtrl>();
 
     }
 
     public void OnTriggerEnter2D(Collider2D Fupa){
 
         Debug.Log ("is Triggered");
 
         if (inOut) {
             camMotion = false;
             stopCam ();
         } else if (!inOut) {
             camMotion = true;
             stopCam ();
         }
     }
 
     void stopCam(){
         mainCamera.cameraStill = camMotion;
     }
 }
 

and this is on the camera:

 public class cameraCtrl : MonoBehaviour {
 
     public Vector2 getMeThere;
     
     Vector3 realPos;
     
     public float camSpeed;
 
     public bool cameraStill;
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
 
         if(!cameraStill){
             movement();
         }else {
 
         }
     }
 
     void movement(){
         realPos.Set(getMeThere.x, getMeThere.y, -10.0f);
         transform.position = Vector3.MoveTowards(transform.position, realPos, camSpeed * Time.deltaTime);
     }
     
 }
 

I have tried everything I can think of, but it keeps on happening. If anyone has had a similar experience and knows of a way that I can fix it please let me know and sorry for the long post

Comment
Add comment · 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 CaptainKirby · Jul 16, 2015 at 01:58 PM 0
Share

It is probably something wrong with your code, try and post it :)

avatar image hexagonius · Jul 16, 2015 at 05:07 PM 0
Share

You're saying you get trigger calls, focus on that. Pause the game when it happens and see which collider or trigger is intersecting the trigger in scene view. If you found it see what the involved colliders do on game start. It won't be triggered if there's no overlap, so there must be one.

avatar image everything_appz · Jul 16, 2015 at 05:20 PM 0
Share

Where are the triggers?

2 Replies

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

Answer by Thomas-Mountainborn · Jul 16, 2015 at 05:43 PM

Since you're not checking what is triggering the trigger, it is most likely some physics object rolling in and out of your trigger. In OnTriggerEnter2D(Collider2D collider), use the collider variable to get the info of what entered your trigger, and only perform the actions if the expected object is in the trigger.

 void OnTriggerEnter2D(Collider2D coll)
 {
   if(coll.GetComponent<charCtrl>() == null) //Checks if the game object that entered is your character by looking for the charCtrl script. Alternatively, you could use tags.
     return;
 
   //Your code goes here.
 }
Comment
Add comment · Show 3 · 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 SylvesterAHansen · Jul 16, 2015 at 06:51 PM 0
Share

sadly I can say that the problem persists. I tried both your code and a version where I checked it up against the tag of the gameobject that holds charCtrl. Nothing seems to be working

avatar image Thomas-Mountainborn · Jul 17, 2015 at 04:33 AM 0
Share

I guess you got it working after all?

avatar image SylvesterAHansen · Jul 17, 2015 at 08:00 AM 0
Share

yes, I got it working after making it check for CharCtrl and the tag of the game object. seems a bit excessive, but it works

avatar image
0

Answer by everything_appz · Jul 16, 2015 at 06:24 PM

Just add a tag to the object that should enter the trigger. Than in the OnTriggerEnter() function add an if statement making sure that the only thing that makes the code run is if it is the proper object setting off the trigger.

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

25 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

Related Questions

Slowing down the speed of my bullets whilst holding in the key. 1 Answer

How to Fire a projectile using the right Thumbstick? 2 Answers

damage script 1 Answer

How could I shoot a chemical in a fire extinguisher? 1 Answer

How to fire a gun in 2d game 2 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