• 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 d.b · Feb 09, 2013 at 11:51 PM · raycastcolliderperformancecoroutinethreads

Stuck at how to code something

Hi

I have a bunch of sphere colliders spead out on the xz plane. from the top i fire a ray at the position of the mouse to intersect hit any of the colliders. This works but there is a massive performance drop with only about 30 colliders in the scene. So

Coroutines, i looked at but dont return a type that is useful to me, i want the gameobject back

Threads also are not useful because they dont allow you to use the unity api, ie. the raycast

so i dont know how to go forward from here.

Im looking for a way of getting the gameobject back without the performance hit Any help would be greatly appreciated.

thanks

Comment
Add comment · Show 5
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 T27M · Feb 10, 2013 at 12:01 AM 0
Share

Unless any physics is being calculated on the object they will be put to sleep, sounds like it could be a coding issue could you post the code you are using to cast the ray and detect the collision? What other scripts/objects do you have in your scene?

avatar image d.b · Feb 10, 2013 at 01:31 AM 0
Share

Hi

Its supposed to be a cell game so there are cells and a nucleus at the moment, The cells have a script on it which controls position via various forces and birth/death animations, the nucleus has a script in it much the similar but also with spawning of the cells.

the code below is from the cell script but only the physics bit you asked for

public class Cell_Ctrl : $$anonymous$$onoBehaviour { void FixedUpdate() {

    GameObject hitGo;
 
    if (Input.Get$$anonymous$$ouseButton(0))
    {        
      hitGo = getHitCell();
    }
 }
 
 public GameObject getHitCell()
 {
    RaycastHit hitInfo;
 
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    Physics.Raycast(ray, out hitInfo, 10);
 
    //Debug.Log (hitInfo.transform.gameObject.name);
 
    return hitInfo.transform.gameObject;
 }

} Thanks for looking into this :D

avatar image d.b · Feb 10, 2013 at 02:09 AM 0
Share

aaawwwwwww man, now i feel really stupid, absolutely bang on the money

it was on a instanced object so it was creating a raycast each for however many cells there are in the scene

Thanks!

avatar image d.b · Feb 10, 2013 at 02:11 AM 0
Share

jogo13`s comment is correct but i cant promote it to the answer :D

avatar image jogo13 · Feb 10, 2013 at 06:36 AM 0
Share

i converted it to an answer, glad it worked!

1 Reply

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

Answer by jogo13 · Feb 10, 2013 at 01:53 AM

Sphere collision is one of the cheapest collisions possible, the performance drop is probably related to your implementation at the moment.

Do you have the raycast code built into every cell? That would mean if you have 30 cells, 30 raycasts are being created. You should only have one raycast check per mouse click. Just create a single gameobject with a new class that handles the `getHitCell(); function.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

BoxCollider vs. RaycastAll 1 Answer

Physics raycast hit offset from where it should be 1 Answer

Changing texture with raycast on iPad 2 Answers

Raycast exit point of collider 0 Answers

Accessing a shadowmap and using it in C# script to turn off colliders? 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