- Home /
[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".
If you need more information I can share it with you.
Thank you, Gerald.
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.
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.
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
I tested the new version and this issue was fixed in Unity 2017.2. Physics.RaycastNonAlloc doesn't allocate memory anymore.
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
Your answer
![](https://koobas.hobune.stream/wayback/20220612133044im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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