Navigate

Saturday, May 26, 2018

FPS -- First-Person Controller and Raycasting Woes

So, this project is a first-person shooter/grocery shopping sim. The player must explore a store, seek out listed items, and then shoot them off the shelves into their shopping cart. Obviously, the first thing I needed was a FPS controller and a form of shooting.

Raycasting is the way I went.

I struggled with getting the racycast/shooting to work as I'd like it. Not 100% satisfied, but it's functional enough to keep moving.

Unity comes with its own First-Person controller, but, for the sake of learning and practicing, I followed an online tutorial on creating a FPS controller from scratch. It's not as robust as the Unity provided option, but it still does more than I need for this Project.

Pretty sure this is the video I used:


According to the many videos I watched, FPS games do not have traveling bullets. Instead, a line/ray is shot/cast out from the camera until it connects with an object/collider: raycasting. The point where the ray makes contact will receive bullet damage instantly. I know games like Battlefield somehow account for bullet travel and bullet drop, but I couldn't find a clear answer on how to get this to cooperate with raycasting.

I did want projectile travel time and not the instant contact that raycasting provides. I'm not 100% sure why I insist on this, but my gut says it's the right path to follow. If testing proves otherwise, I'll happily change it.

Within the tutorial (which I followed on Unity's website) they have a ray cast from the camera. This ensures a centered hit. Then, a second ray is cast form the gun-muzzle to the exact hit point. I'm not going to mess up my code to show the errors I ran into with this--but I will bore you with great detail in text :D.

Firstly, once I had followed all tutorials to perfection(ish), I began to play around--firing pink lasers (raycasts) from gun-muzzle to centered contact-point. But then, seemingly random, the laser would fire backwards. I couldn't understand it. I adjusted code so that the laser did not disappear once fired and discovered that it didn't just fire backwards, but it was firing at my player-avatar's head!

Since I didn't want instant hits from raycasting, but rather a traveling object, I decided I would disable the raycast to avoid shooting myself. But then I noticed another problem, my trageting was no longer centered, it was off-center. This is the obvious result of the muzzle being off-centered (per classic FPS style) and the projectile instantiating from that point. But sadly, it also meant I couldn't bypass the suicide-feature inherit in my raycasting.

After weeks of frustration and refusal to open the project, I found my answer. I don't know if this was mentioned in any of the tutorials I watched and I just didn't catch it (as I tend to work as I listen/watch), but you can actually set layers for objects to not receive raycasting hits. I simply put the player avatar on this layer and now I never shoot myself in the head!


It's not perfect for Battlefield but it'll work for an FPS Shopping Simulator.


*You'll see a red-sphere appear against the wall. This sphere is instantiated by the raycast. You will then see a grey sphere travel from the gun-muzzle to the location of the red sphere. This grey sphere is the projectile that actually interacts with objects in the project.

No comments:

Post a Comment