#Class method call
1 messages · Page 1 of 1 (latest)
You are right, it is always a good idea to cache the results of expensive calls like this, and call them as infrequently as possible (if you can get away with calling it just once, you should absolutely do that)
How you approach/design this though, will depend on what it is you're doing. I don't have much context for your code, so I don't know what will work for you, but one thing I could suggest is caching the result in a property that other behaviours have access to
For example:
public bool IsHit { get; private set; }
public void DoCheck()
{
IsHit = Physics.Raycast(...); // etc, you get the idea
}
and then you just call the method once, and whenever you need the result in future you just check theClass.IsHit
NB: I don't imagine ScreenPointToRay being that much of an issue though, that's not doing any physics checks. That's just doing matrix math, converting a screen point to a couple of vectors. And sure you should also try to do this as few times as possible because it's redundant math that can be avoided if you already have the result. But this specific case doesn't seem so bad