• 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
Question by irilox · Oct 20, 2014 at 12:07 PM · raycastontriggerenterinteractioninteractive

Raycast and OnTriggerEnter performance

Hi.

I have several small objects the player (FPS) can interact with. Examples:

  • a door and the player can interact with the doorknob to open the door

  • a machine with a button on it to power on the machine

  • a weapon on the ground to pick it up

I shoot a raycast from the players view to check if the player is looking at the object

What is best performance wise:

1) Constantly shoot the raycast?

2) Have a OnTriggerEnter on every interactive object and shoot a raycast only when the player enters the trigger?

Comment

People who like this

0 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 Wisearn · Oct 20, 2014 at 12:13 PM 0
Share

Number 2 would be best performance-wise.

avatar image b1gry4n · Oct 20, 2014 at 01:36 PM 0
Share

My guess is you are pressing a button to interact correct? Even better than 1 and 2 is to cast a ray only when the "Interact" button is pressed and do a check from there as to what the player has interacted with

with this method you arent constantly casting unnecessary rays and you dont have to calculate trigger collisions other than the "Interaction raycast"

If you are looking for a system where a UI element will pop up and tell the player "Open door", "Press button", etc wisearn is right, #2 would be best.

avatar image irilox · Oct 20, 2014 at 02:12 PM 0
Share

There is indeed a UI element that tell the player that he can interact with the object if he is not to far away from it.

My guess was also the #2 would be the best to do. But I was not sure if "OnTriggerEnter" would use a lot of performance compared with a constant raycast. But you probably need a lot of "OnTriggerEnter" in scene to have performance issues with it.

Thanks for the answers.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by robertbu · Oct 20, 2014 at 04:46 PM

I believe you are putting your performance efforts in the wrong place. Colliders or Raycasts are pretty efficient. They are the backbone of many apps. When I have these kinds of questions, I run a test to see what I get. So here is a basic FPS script:

 #pragma strict
 
 private var data : float[] = new float[160];
 private var i = 0;
 private var fps : int;
 
 function Update () {
     data[i] = Time.time;
     i = (i + 1) % data.Length;
     fps = 0;
     for (var j = 0; j < data.Length; j++) {
         if (data[j] > 0 && data[j] >= Time.time - 1.0)
             fps++;
     }
 }
 
 function OnGUI() {
     GUI.Label(Rect(10,10,100,50), fps.ToString());
 }

And here is a test script. It creates a set of planes and then Raycasts() against those planes. The spacebar doubles the number of Raycasts().

 #pragma strict
 
 private var casts = 1;
 private var planes = 200;
 
 function Start() {
     for (var i = 0; i < planes; i++) {
         var go = GameObject.CreatePrimitive(PrimitiveType.Plane);
         go.AddComponent.<MeshCollider>();
         go.transform.rotation = Random.rotation;
         go.transform.position = Random.insideUnitSphere * 10.0;
      }     
 
 }
 
 function Update () {
 
     var hit : RaycastHit;
     for (var i = 0; i < casts; i++) {
         Physics.Raycast(transform.position, Random.onUnitSphere, hit);
     }
     
     if (Input.GetKeyDown(KeyCode.Space)) {
         casts *= 2;
     }
 }
 
 function OnGUI() {
     GUI.Label(Rect(10,60,50,100), casts.ToString());
 }


Put both scripts on an empty game object or the camera and hit play. Start hitting the space bar to increase (double) the number of casts. See when you get an FPS drop. For me in the editor, it was 2048 casts when I first see a drop, but for an accurate test, you would want to use object representative of the complexity you have in your game, and you would want to run on the target device. But my point is, that one continuous Raycast() is not a big deal.

And if you ended up with enough raycasts and/or objects for raycasting to become a problem, then there are ways to mitigate the issue.

  • Make sure you have the objects that you want to detect on their own layer and use a layer mask in the Raycast().

  • Use a distance check to avoid raycasting when nothing is in range.

  • Use an angle check to make sure the player can 'see' the object

The Unity Wiki also has an FPS script:

http://wiki.unity3d.com/index.php?title=FramesPerSecond

Comment

People who like this

0 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 irilox · Oct 21, 2014 at 07:05 AM 0
Share

Thanks robertbu, I will definitely check this out !

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to turn real life object into gameobject 1 Answer

Yet another collision detection mystery 1 Answer

Capturing a point 2 Answers

My game after being built has some really weird glitches? 0 Answers

Terminal Typing 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