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 reptilebeats · Jul 10, 2012 at 08:37 PM · raycasting

raycasting performance wise

hi im still trying to learn the best way of raycasting, im currently using it with my own gui system and was wandering which method is the best perfomance wise to cast a ray?

so at the moment my script looks like this

  for(var i = 0; i <Input.touchCount; ++i){
  var touch = Input.GetTouch(i);
  var ray = Camera.mainCamera.ScreenPointToRay (touch.position);
  var hit : RaycastHit;
  var guiLayer = 1 << 10;
  
  if(Physics.Raycast (ray,hit,Mathf.Infinity,guiLayer)){
 // Debug.Log(hit.collider.gameObject.name);
   
  if(hit.collider.gameObject == Object){ 
     do stuff
 
  }
  }

}

but i have seen another method of using switch,case any advice

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 reptilebeats · Jul 10, 2012 at 08:38 PM 0
Share

also on some things i will have a touch phase as well

avatar image reptilebeats · Jul 10, 2012 at 09:26 PM 0
Share

ins$$anonymous$$d of posting a new question i was also wandering if it would be better to use a number ins$$anonymous$$d of infinity

avatar image Statement · Jul 10, 2012 at 09:51 PM 2
Share

Is it very expensive in your particular scene? What sort of colliders are you using? Changing from infinity will just check less objects, so if you don't $$anonymous$$d your fingers "reaching out as far", then it could potentially be a little bit faster, but profile it to be sure.

avatar image reptilebeats · Jul 10, 2012 at 11:35 PM 0
Share

currently i dont own pro, so trying to learn the best approach to doing things, then i dont pick up bad habits. ive had to change my entire gui system just to reduce draw calls. game ran fine no lag on phone but the game itself is very fast moving with a lot of physics being used, explosions and stuff. so get the performance right first time will help when i want to make it on older devices. (wont drain the battery as much as well)

on one level i had almost a hundred draw calls, i was shocked it even ran on a mobile so well, doubt it would be the same for a phone like the 3gs, which is still a very used phone.

but for my gui its just simple box colliders about 10 controls all supporting mult touch. inside a separate camera orth view i think. infact if u use ngui its basically that but with my own script to call the colliders.

avatar image reptilebeats · Jul 10, 2012 at 11:36 PM 0
Share

just to note the 100 draw calls have been significantly reduced, i just wanted to see if it could handle it

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bovine · Jul 11, 2012 at 09:04 AM

For 3D touches I have an IInteractiveObject interface that other scripts can implement to handle touch events and instead of comparing objects and then 'doing stuff' I look for the component on the collider and if present I call the relevant method, effectively delegating the behaviour of that touch action to the object that was touched (or the type rather, as I have only a handful of interactive object types). Although I use this for 3D, you could use the same kind of thing for UI.

My code looks something like:

     BVInteractiveObject interactive = object_hit.GetComponent<BVInteractiveObject>();
     if(interactive != null && interactive.ClickDistance >= hitInfo.distance) interactive.Clicked();

Apologies as this is C# code - I don't know the equivalent javascript.

As you're only ray-casting a particular layer, you probably want all colliders in that layer to be considered, so it is likely to make little difference having infinity or a particular depth. If you extend to ray-casting into the 3D view, then you probably want to be able to configure which cameras to use, which layers to consider and how deep the touch should go.

As an aside...

For your GUI, it is a common practise to create a texture atlas so that you can (in theory) draw the whole GUI in 1 draw call - this is a very good way to improve performance on mobile as there are fewer draw calls and state changes (changing the material for each button for example). I don't know if you're doing this?

Comment
Add comment · Show 10 · 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 whydoidoit · Jul 11, 2012 at 09:08 AM 1
Share

This should be the equivalent JS:

   var interactive = object_hit.GetComponent(BVInteractiveObject);
   if(interactive != null && interactive.ClickDistance >= hitInfo.distance) interactive.Clicked();
avatar image Bovine · Jul 11, 2012 at 09:12 AM 0
Share

It's worth noting that finding a component is probably actually less performant than comparing GameObjects, but a:) you're not going to be doing this hundreds of times a frame and b:) comparing GameObjects doesn't scale and you probably won't be able to re-use your UI code later on.

avatar image reptilebeats · Jul 11, 2012 at 12:07 PM 0
Share

Thanks for the information im currently learning csharp and java, i was thinking of looking for the object inside a tag ins$$anonymous$$d but either way it stores in a varable. And yes the whole idea of this is so that i can use an atlas to reduce game size and draw calls.

Thanks for a detailed description of checking a hit a different way.

avatar image Bovine · Jul 12, 2012 at 06:26 PM 1
Share

Yes, if you put the things you want to be able to 'pick' with the mouse or touch on a mobile device, you reduce the number of colliders that need to be considered, so it will give you some performance gain - it's just good practise: do it at the start and you don't have to go through and retro fit it later if you have performance issues.

avatar image whydoidoit · Jul 13, 2012 at 07:52 AM 2
Share

I figure I'm trying for a balance between what I learn and what I help with :) Damn gamification though... $$anonymous$$eeps you co$$anonymous$$g back ;)

Show more comments

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

8 People are following this question.

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

Related Questions

Multiple Camera.main.ScreenPointToRay 1 Answer

How to destroy a gameObject when hit by Raycast? 4 Answers

Shotlock System 1 Answer

Ground Hugging Vehicle 1 Answer

What effects/controls a ScreenPointToRay() ray? 1 Answer


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