- Home /
3rd person camera pointing system
Hello everyone.
I've figured out how to make a working "aiming" system for my 3rd person shooter controller. I've got a raycast from the camera, and the bullets go from the player to where the raycast (where the camera is looking, the center of the screen) has collided, so the bullets always go to where the center of the screen is, like on most other third person shooter games (if not all of them).
The problem is when the raycast doesn't collide, like when i'm pointing to the sky. Where should the character's gun point in that case?
Thank you very much
Answer by Wolfram · Jun 04, 2012 at 10:51 PM
You can constrain the max distance of your Raycast with the "distance" parameter (for example, use a value around the farClip value of your camera as distance). Then if there is no hit, origin+distance*direction marks the endpoint of your ray.
http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html
Thanks, that helped a lot ^^ I already thought to make the gun point the end of the ray, but as Unity didn't provide any built-in method for calculating this, I thought it was difficult and didn't stop to think about it... xD Now I see it was just some easy math (which actually I'm doing right now at school, so what a fail from me)
Anyway, thank you
Answer by Piflik · Jun 04, 2012 at 10:14 PM
I would create an invisible boundary that encompasses your whole scene. When you ray doesn't hit anything in the scene, it hits the boundary.
Answer by jomath229 · Jan 13, 2015 at 01:18 AM
just put a cube game object aligned with the camera really far maybe something like 1000 units and remove the renderer so the raycast is always going to be hitting something.
Your answer
Follow this Question
Related Questions
How do I fix this jitter problem? 0 Answers
Camera collision 2 Answers
Rotate charcter with camera? 1 Answer
Help with a simple 3rd person camera? 1 Answer
How to make camera position relative to a specific target. 1 Answer