Facing the Camera: “Physics.Raycast” vs “Vector3.Dot” in Unity3D – PART 2

After further optimization testing, I felt I had enough content to write a new blog post, to follow up the original article from earlier. The big reason is because the results I came up with were wrong, due to a major flaw in my testing environment. This article will cover physics colliders parented to other objects, dot product vs raycasting, and a little bit of multithreading in Unity3D.

A lot of random-looking dancing objects. This is harder to compute than you think!

Continue reading

Facing the Camera: “Physics.Raycast” vs “Vector3.Dot” in Unity3D

I had a great time this week, having been given the opportunity to present a talk at IGDA – Ann Arbor in Ypsilanti, Michigan this month. I’ve been attending their meetups for over a year, and they consistently get a great turnout and great discussions. This time, group was a good size, the audience seemed interested, and I didn’t mumble as much as I tend to. Of course, the talk was about “3D Cel Animation.”

But with many developers and programmers in the room, they brought up some suggestions to optimize how I check which ‘perspective’ of a character should be shown at a given frame. A common suggestion was to use “dot product” at a low-level instead of relying on “Physics.Raycast.” Even years ago, this had been suggested to me. I had fears about its’ efficiency, but after a few one-on-one discussions, I agreed it might turn out to be more efficient, and it wouldn’t be hard to implement.

I felt like such a fool. For 5 years, I’ve preached the brilliance and simplicity of my design! Was an alternate solution so obvious, and really better?

So I gave it a try. And the results surprised me.

A visual comparing “RaycastAll,” “Raycast” and “Dot Product” as they relate to knowing what orientation a character is in.

Continue reading