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 /
  • Help Room /
avatar image
0
Question by JNGRichardson · Nov 25, 2021 at 12:01 PM · collisioncollision detectiontimers

Detecting a Continuous Collision/Player Position and Reseting Timers,Detect a collision for a certain number of seconds

Hi all,

I've been tearing my hair out with this, and while there seem to be a few very similar questions out there I've yet to really figure this out.

The setting: The player needs to remain in a certain area for 3 seconds in order to take a photo. Currently I'm using a plane with a mesh trigger as the 'photo zone.' If they stay touching the plane for 3 seconds, the photo is taken (i.e. bool photoTaken = true). If they leave the photo zone before the 3 seconds are up, the timer resets.

I found a very similar question here and used that code to get started. It basically works, but annoyingly photo-taking 'process' seems to start every frame, leading to enormous spam in the console. I'd like to have the process start once for each entrance into the zone, rather than firing continuously.

I'm pretty new to Unity and C# so I'm sure I'm doing something a bit silly. Posting my code (attached to the player GameObject, minus other things like movement for brevity) for reference.

Thanks in advance for any advice you might have!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     public bool photoTaken = false;
     public float photoCountdown = 3.0f;
     public bool inPhotoZone = false;
 
     // Start is called before the first frame update
     void Start()
     {
         playerRb = GetComponent<Rigidbody>();
     }
 
     // Update is called once per frame
     void Update()
     {
         // Collision timer
         if (inPhotoZone == true)
         {
             photoCountdown -= Time.deltaTime;
             if (photoCountdown < 0)
             {
                 photoCountdown = 0;
             }
         }
         else if (inPhotoZone == false)
         {
             photoCountdown = 3f;
         }    
     }
 
     // Check for searchlight and photo zone collisions
     private void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag("Searchlight"))
         {
             Debug.Log("!");
         }
         else if (other.gameObject.CompareTag("Photo Zone"))
         {
             Debug.Log("Steady...");
             inPhotoZone = true;
         }    
     }
 
     // Check player remains in photo zone
     private void OnTriggerStay(Collider other)
     {
         if (other.gameObject.CompareTag("Photo Zone") && inPhotoZone == true)
         {
             Debug.Log("Ho-o-old it...");
             if(photoCountdown <= 0)
             {
                 photoTaken = true;
                 Debug.Log("*snap!*");
             }
         }
     }
 
     // Reset timer if player leaves photo zone
     private void OnTriggerExit(Collider other)
     {
         if (other.gameObject.CompareTag("Photo Zone") && photoTaken == false)
         {
             Debug.Log("Darn it! Hold steady won't you?");
             inPhotoZone = false;
         }
     }
 }


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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Dsiak · Nov 25, 2021 at 11:03 PM

Hi @JNGRichardson thats a very good formatted question. The reason you are getting spammed is because once it reaches the condition to snap a pic it will keep on reaching the condition every frame, therefore snapping every frame. Try making it so the condition is only true for one frame, you already half way there all I did was add another boolean check to the snapping.

     private void OnTriggerStay(Collider other)
     {
         if (other.gameObject.CompareTag("Photo Zone") && inPhotoZone == true && photoTaken == false)
         {
             Debug.Log("Ho-o-old it...");
             if (photoCountdown <= 0)
             {
                 photoTaken = true;
                 Debug.Log("*snap!*");
             }
         }
     }

Now that leaves you with a "Ho-o-old it..." spam, so keeping true to the methods I'll add another check to the "Ho-o-old it..." text:

     private void OnTriggerStay(Collider other)
     {
         if (other.gameObject.CompareTag("Photo Zone") && inPhotoZone == true && photoTaken == false)
         {
             if (!isHoldingIt)
             {
                 Debug.Log("Ho-o-old it...");
                 isHoldingIt = true;
             }
             
             if (photoCountdown <= 0)
             {
                 photoTaken = true;
                 Debug.Log("*snap!*");
             }
         }
     }

That should give you a single stream of: alt text

But if you allow me to be ever so nosy I'll much rather you use Coroutines, that way you can more easily pass it parameters for different photo zones and you can make use of the "yield return new WaitForSeconds();" The code with Coroutines would be something like this:

     // Check for searchlight and photo zone collisions
     private void OnTriggerEnter(Collider other)
     {
         Debug.Log("Enter Trigger"); 
         if (other.gameObject.CompareTag("Searchlight"))
         {
             Debug.Log("!");
         }
         else if (other.gameObject.CompareTag("Photo Zone"))
         {
             Debug.Log("Steady...");
             inPhotoZone = true;
             StartCoroutine (HoldingItCoroutine())
         }
     }
     IEnumerator HoldingItCoroutine()
     {
         Debug.Log("Ho-o-old it...");
         yield return new WaitForSeconds(3);
         photoTaken = true;
         Debug.Log("*snap!*");
     }
 
     private void OnTriggerExit(Collider other)
     {
         if (other.gameObject.CompareTag("Photo Zone") && photoTaken == false)
         {
             StopCoroutine(HoldingItCoroutine());
             Debug.Log("Darn it! Hold steady won't you?");
             inPhotoZone = false;
         }
     }


untitled.png (19.4 kB)
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

243 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 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 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 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

Collision issue, Plz help 1 Answer

OnTriggerExit not working 0 Answers

Having the worst time making OnCollision work... Strange. 0 Answers

Unity 2D does not detect collisions 0 Answers

How i can fix a player controller bug? 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