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
1
Question by CryptoQuick · May 15, 2011 at 05:45 PM · javascriptraycastraycasthitrecursion

Recursive Raycast Spatial Search

When the player clicks on an object, I raycast to that object, find what it hit, then run a search. This Search function, which is attached to the object itself, runs the following code in several directions around itself:

function Rayaround (dir, color, score) {
    var visitnum = -1;
    var hit : RaycastHit;
    if (Physics.Raycast (transform.position, dir * 0.1, hit)) {
        visitnum = hit.collider.gameObject.GetComponent(Tile).num;
        if (hit.collider.name == color && !index.Contains(visitnum)) {
            index.Add(visitnum, true);
            score++;
            score += hit.collider.GetComponent(Tile).Search(color, score);
            Destroy(hit.collider.gameObject);
        }
    }
    return score;
}

Some clarification: This code is called Tile and attached to the colliders I'm hitting, index is static and shared amongst all Tile objects, and num / visitnum are just unique numbers given to each object when they're first created.

So, the major issue here is that score gets really huge when I run this. Like, two billion huge. Huge enough to overflow a signed integer (great idea for a 'yo mama' joke, btw). There's about a thousand objects here, but this should only be searching through a dozen of those, if not less.

I'm not entirely sure why that number gets so large. The number is not incremented anyplace else. The program takes a noticeably long time to run through this piece of code.

I'm being careful not to delete things that search until only after they've searched, and I've created a hashtable of objects I've visited before so they don't get visited again... It feels like I'm doing all the right things, yet I'm getting nowhere towards solving this problem. Thoughts?

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 Eric5h5 · May 15, 2011 at 06:04 PM 0
Share

Probably not related to the problem, but one thing you should always do is define the type of variables used in the function. function Rayaround (dir : float, color : String, score : int), or whatever the types actually are.

avatar image CryptoQuick · May 16, 2011 at 04:23 AM 0
Share

Yeah, I noticed that after I posted this. :) dir is a Vector3, of course ;)

avatar image _Petroz · Jul 12, 2011 at 02:12 AM 0
Share

You are either incrementing score by a small amount many times or a very large amount one or a few times. Put something like:

  Debug.Log("score is " + score); 

in the inner if statement, one before and one after the "score += ..." line. That should tell you which.

0 Replies

· Add your reply
  • Sort: 

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

1 Person is following this question.

avatar image

Related Questions

Switch GameObjects Tags with javascript 1 Answer

Make tranform follow raycast 1 Answer

detecting reycast collisions 1 Answer

Raycast Distance Help 1 Answer

When I click, my enemy doesnt lose health... 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