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 metaphysician · Jul 24, 2012 at 06:53 AM · raycastingtimingcounting

How to measure rate of Raycast hit events?

hi folks i have a dilemma and just looking for some newbie friendly advice on this. i want to use the measurement of raycast hit events to control music.

i have a a situation where i'm using a Raycast sent from the FPP object in order to see if it detects a robot. i have the various surfaces tagged so that it prints an event depending on which type of object was hit within its radius. so far so good. it generally works reasonably well. now, what i want to do it measure the frequency of events received so that the more events are received the more intense the music is and the lesser number received, the less intense the music is. here's the current raycast code. it's largely snarfed from another Unity Answers question:

 player = GameObject.Find("FirstPerson Player");
 var hit : RaycastHit;
 var fwd = player.transform.TransformDirection (Vector3.forward);
     if (Physics.Raycast (player.transform.position, fwd, hit, 30.0)) {
        print("hit something!");
        print(hit.collider.tag);
        if(hit.collider.tag == "Wall"){
  print("hitting a wall");} 
  if(hit.collider.tag == "Robot"){
  print("hitting a Robot!"); 
  targetObject = GameObject.Find("audioTest");
  deaths=1;
  SendMessage ("TensionMusic", deaths);} 
  }

so are we talking time.deltaTime or what function? i've looked through the Unity scripting stuff, but maybe it's a C# syntax function question? i don't care about elapsed time since the beginning of the game or network time. just to measure time difference between the hits. i could also count the number of hits in say, 5 seconds, but i'm not sure how to do that either.

i have looked around and i haven't found anything matching my search terms, so if anyone finds a forum post or Answers that deals with this or would care to point me in the right direction i'd be very grateful!

Comment
Add comment · Show 1
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 ScroodgeM · Jul 24, 2012 at 07:14 AM 0
Share

'measure the frequency' - do you want to measure frequency per second? per frame? or for last $$anonymous$$ute? for example if you want measure it in a frame, so you need to declare some counter, reset it in Update method and increase per hit, then at after hits are ended send collected data to auidosource

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Seth-Bergman · Jul 24, 2012 at 07:14 AM

well, to count the number of events in a set period, i use a simple function:

 function CountHits(){
 hitCount++;
 yield WaitForSeconds(5);
 hitCount--;
 }

but you need to keep in mind that every frame you're facing say, the wall, the raycast is hitting it, so you probably want to store the hit object to check against the current hit:

 if (Physics.Raycast (player.transform.position, fwd, hit, 30.0)) {
 if(hit != lastHit){
 CountHits();
 print("hit something!");
 print(hit.collider.tag);
 //...etc
 lastHit = hit;
 }

or something..

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

Answer by metaphysician · Jul 24, 2012 at 08:11 AM

thanks seth - this gets me started i think, though i'm a bit curious what the hitcount-- does after the yield statement. based on my (admittedly imperfect) knowledge of Unity scripting it seems your CountHits() will increase until the 5 seconds is up and then it hits the decrease? i guess i'd want to reset the count every 5 seconds - let me see if i can adapt this. would this work?

 function CountHits(){
 hitCount++;
 yield WaitForSeconds(5);
 //store count after 5 seconds?
 hitCount=StoredRate
 //reset count? 
 hitCount=0;
 }

if i'm wrong and the function indicates what happens in each frame then it means each frame would increment upwards wait 5 seconds then decrement, but that doesn't seem useful.

i'm mainly interested the number of times per second (or whatever time) i get the hit on the robot object, so it doesn't have to be a raycast hit per se - i could count messages and basically ignore any other types of hits (like on the wall). so maybe adapt the latter half...

 if (Physics.Raycast (player.transform.position, fwd, hit, 30.0)) {
 if(hit != lastHit){

so what you're doing here is filtering out repeats of the same tag? i'd definitely want repeated events but just sorted out, so maybe something like this?

 if(hit.collider.tag == "Robot"){
 CountHits();
 print(StoredRate);
 }
 //...etc
 lastHit = hit;
 }

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 metaphysician · Jul 25, 2012 at 03:58 AM 0
Share

well, this is interesting - still haven't gotten it to work but i have it printing a bunch of positional information for each robot tag hit. whatever i'm using to print the accumulated data count isn't achieving anything but it obviously does generate hit data. the console itself puts the events out of order as well so it makes it confusing to track. anybody have any more ideas?

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast stops working for big scaled objects sometimes 2 Answers

How to make a raycast follow terrain? 1 Answer

Strange 'Glitch' while trying to apply the built-in layer "Ignore Raycast" via code 0 Answers

How can I select a UI element with a raycast from another UI element? 2 Answers

How can I instantiate a different sprite depending on where Raycasting goes? 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