Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 G3R1 · Aug 19, 2017 at 01:57 AM · raycastmemoryallocation

[SOLVED] Physics.RaycastNonAlloc allocating memory ?!

Hi,

I just found out that I can use Physics.RaycastNonAlloc to prevent memory allocation. I am using a script to manage the input in my game and everything works fine except for allocating memory each time a mouse or touch input is made. The thing is that I can't find a single script even in Unity's documentation on how to use Physics.RaycastNonAlloc, so if someone could help me I'd be grateful. I just need to convert this script using Physic.RaycastNonAlloc and not Physics.Raycast to execute a function in my game.

Here is the script I am using now :

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class TouchInput : MonoBehaviour {
 
     // Singleton     
     private  static TouchInput instance;      
 
     // Construct     
     private TouchInput() {}      
 
     //  Instance     
     public static TouchInput Instance {         
         get {             
             if (instance == null)
                 instance = GameObject.FindObjectOfType (typeof(TouchInput)) as TouchInput;             
             return instance;         
         }  
     }
 
     public LayerMask touchInputMask;
     private RaycastHit hit;
 
     Ray ray;
     GameObject recipient;
 
     public delegate void OnClickEvent(GameObject g);
     public event OnClickEvent OnTouchDown;
 
 
     void Update () 
     {
 
         #if UNITY_EDITOR
         if (GameControl.control.GetComponent<GamePlay>().isPlaying) 
         {
             if (Input.GetMouseButton(0) || Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0) )
             {
                 ray = GetComponent<Camera> ().ScreenPointToRay (Input.mousePosition);
 
                 if (Physics.Raycast (ray, out hit, touchInputMask))
                 {
                     recipient = hit.transform.gameObject;
                 }
                 if (Input.GetMouseButtonDown(0))
                 {
                     OnTouchDown(recipient);
                 }
             }
         }
 
         #endif
 
         if (GameControl.control.GetComponent<GamePlay>().isPlaying) 
         {
             if (Input.touchCount > 0)
             {
                 foreach (Touch touch in Input.touches)
                 {
                     ray = GetComponent<Camera> ().ScreenPointToRay (touch.position);
 
                     if (Physics.Raycast (ray, out hit, touchInputMask))
                     {
                         recipient = hit.transform.gameObject;
                     }
                     if (touch.phase == TouchPhase.Began) 
                     {
                         OnTouchDown (recipient);
                     }
                 }
             }
         }
     }
 }
 

You can also see the image below to see how much memory is this script allocating each time I click over the object which has the same layer as "touchInputMask". alt text

If you need more information I can share it with you.

Thank you, Gerald.

screen-shot-2017-08-19-at-35244-am.png (325.7 kB)
Comment
Add comment · Show 1
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 G3R1 · Aug 19, 2017 at 09:50 AM 0
Share

Guys if anyone has an example on how to use Physics.RaycastNonAlloc it would really help me because this is the only thing left to prove if my game stuttering is caused by garbage collection or it is a Unity Engine bug. So, if someone has any idea, please share it here. Thank's.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by G3R1 · Aug 19, 2017 at 11:08 AM

I found out by myself (maybe I'm not using it right) how to use Physics.RaycastNonAlloc. The strange thing is that Physics.RaycastNonAlloc is allocating memory even more than Physics.Raycast. I am pasting the codes I am using again so you can have a look:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class TouchInput : MonoBehaviour {
 
     // Singleton     
     private  static TouchInput instance;      
 
     // Construct     
     private TouchInput() {}      
 
     //  Instance     
     public static TouchInput Instance {         
         get {             
             if (instance == null)
                 instance = GameObject.FindObjectOfType (typeof(TouchInput)) as TouchInput;             
             return instance;         
         }  
     }
 
     public LayerMask touchInputMask;
     private RaycastHit hit;
 
     private RaycastHit[] results;
 
     string tagName = "TouchInput";
 
     Ray ray;
     GameObject recipient;
 
     public delegate void OnClickEvent(GameObject g);
     public event OnClickEvent OnTouchDown;
 
     void Start()
     {
         results = new RaycastHit[255];
     }
 
     void Update () 
     {
 
         #if UNITY_EDITOR
         if (GameControl.control.GetComponent<GamePlay>().isPlaying) 
         {
             if (Input.GetMouseButton(0) || Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0) )
             {
                 ray = GetComponent<Camera> ().ScreenPointToRay (Input.mousePosition);
 
                 if(Physics.RaycastNonAlloc(ray, results, touchInputMask) > 0)
                 {
                     /*foreach(RaycastHit hit in results)
                     {
                         if(hit.collider != null)
                         {
                         //recipient = hit.transform.gameObject;
                         }
                     }*/
                 }
                     
                 /*if (Physics.Raycast (ray, out hit, touchInputMask))
                 {
                     recipient = hit.transform.gameObject;
                 }*/
                 if (Input.GetMouseButtonDown(0))
                 {
                     OnTouchDown(recipient);
                 }
             }
         }
 
         #endif
 
         /*if (GameControl.control.GetComponent<GamePlay>().isPlaying) 
         {
             if (Input.touchCount > 0)
             {
                 ray = GetComponent<Camera> ().ScreenPointToRay (Input.GetTouch(0).position);
 
                 if (Physics.Raycast (ray, out hit, touchInputMask))
                 {
                     recipient = hit.transform.gameObject;
                 }
                 if (Input.GetTouch(0).phase == TouchPhase.Began) 
                 {
                     OnTouchDown (recipient);
                 }
             }
         }*/
     }
 }

I hope someone from Unity can have an answer for this cause nodody seems to care about this forum and the questions that are being asked here. :P Thank's.

alt text


screen-shot-2017-08-19-at-10932-pm.png (370.3 kB)
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 G3R1 · Aug 19, 2017 at 11:29 AM 0
Share

I hope that it is related to this issue number "https://issuetracker.unity3d.com/issues/physic2draycaster-allocates-24-bytes-each-frame" which is now fixed in Unity 2017.2. If it is fixed I will accept my own comment as an answer. :'D

avatar image G3R1 G3R1 · Aug 19, 2017 at 01:14 PM 0
Share

I tested the new version and this issue was fixed in Unity 2017.2. Physics.RaycastNonAlloc doesn't allocate memory anymore.

avatar image
0

Answer by mikeamer007 · Feb 08, 2018 at 07:21 AM

I've you try this int nbHits = Physics.RaycastNonAlloc(ray, results, touchInputMask) ; for(int i=0;i

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.

Follow this Question

Answers Answers and Comments

140 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 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 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 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 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 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 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 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 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 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

How to disable referenced Prefabs being preallocated in memory?? 0 Answers

xcode instruments memory allocation keeps adding up, empty scene! 1 Answer

To destroy or not to destroy? 1 Answer

Fatal error! System out of memory! Unity 3D 1 Answer

Best way to calculate sum of custom object holding numbers and returning sum as that object instance 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