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 Tim-Michels · Nov 21, 2012 at 01:08 PM · raycastpositionperformancerayworld

Performance issue Raycast each frame

I'm creating a game for mobile devices where I use a Raycast to get the world position on an object where the player touches the screen. For example, I have a ground plane, and if I touch the screen, I want to know where exactly in the 3D world my finger is touching the plane.

Here's my code for the raycast:

 Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
 RaycastHit hit;
 _Floor.collider.Raycast(r, out hit, 50);
 
 _TargetPos = hit.point;

Ideally, I want to do this every frame, but it seems to be quite a demanding piece of code.

_Floor is the object where I want to get the world position on, so I only raycast on this collider. I really thought this would be a decent way of doing the calculation, but my framerate drops under 15 on my Android, so I'm actually looking for alternatives/optimizations to get the same calculation, but with a better framerate.

Perhaps I'm missing something simpler, because I only need to now the Vector3 where my mousePosition intersects with a specific 3D mesh.

Any help/tips are greatly appreciated, thanks in advance ;)

ps: I also tried a system where my Raycast is done every 0.x seconds, but it doesn't stop the framedrop and it gives less good results.

Comment
Add comment · Show 7
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 Kryptos · Nov 21, 2012 at 02:08 PM 0
Share

Ask yourself this question: When do you need to cast the ray? Each frame? Only when the player touch the screen?

avatar image Tim-Michels · Nov 21, 2012 at 02:15 PM 0
Share

I only cast the ray when the player touches the screen, and I've tried both ways: 1: cast a ray each frame the player touches, and 2: cast a ray every 0.2 seconds when player is touching.

$$anonymous$$y main issue here is to make the calculation itsself simpler.

avatar image CodeMasterMike · Nov 21, 2012 at 03:00 PM 0
Share

Every 0.2 seconds isn't that much different from every frame, try something more like every 1, 2 or 5 seconds and see if there is a difference.

If not, maybe it's something else that slows down your project. Is there something more to your calculation code than you have posted above?

avatar image Tim-Michels · Nov 21, 2012 at 03:09 PM 0
Share
  1. seconds is a lot different from every frame (5 x per second), and also it's not good enough for what I want. The calculation is intended to drag an object over another object, so casting every 5 seconds would really suck...

The raycast is basically all I'm doing right now, so I really need to find a better way of calculating the world position.

In case the calculation of my Raycast is allready "the most optimized way" of doing that calculation, it would also be nice to know...

I just want to make sure there isn't a simpler way of calculating a world point on a mesh where my mouseposition is.

avatar image Kryptos · Nov 21, 2012 at 03:15 PM 2
Share

A single raycast each frame should not drop the performance.

Ask yourself this other question: do you move a static collider, a kinematic rigidbody or a non-kinematic rigidbody?

$$anonymous$$oving a static collider or a non-kinematic rigidbody is pretty expensive, especially on mobile device.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Xtro · Jul 31, 2013 at 04:24 PM

Kryptos is right. Single raycast each frame is not a big performance deal. Moving static colliders is!

If you still want a solution for your original question,

Try to do it in FixedUpdate event rathar than Update event.

Notice that, this only helps when your actual framerate is bigger than the fixed time rate defined in Time settings. If your framerate is lower than fixed time rate, there is no difference between FixedUpdate and Update events. They both are called in the same rate which is your framerate.

Comment
Add comment · Show 2 · 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 Nicolas-Liatti · Jul 31, 2013 at 11:41 PM 0
Share

It's ok I managed to find the problem, and it may be useful for some people: it didn't come from the ray casting, but from OnGUI which had too heavy stuffs to compute!

avatar image Xtro · Aug 08, 2013 at 07:27 PM 0
Share

You should convert your comment to an answer.

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

14 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

Related Questions

Why Does the Ray change my object position? 1 Answer

How to set position of raycast higher? 1 Answer

Screen point to point on Plane WITHOUT Raycast? 1 Answer

Changing position of a RayCast 1 Answer

Camera Ray - NullReferenceException: Object reference not set to an instance of an object 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