Hey, I’m looking for an easier way to make a chase camera, currently my attempt uses UnityEngine.Physics.Raycast, starting with a maximum range, then iterating down at a step of 0.01f until it is no longer being blocked, then doing some real clumsy math with sin and cosine to achieve the desired viewing angles, ofcourse needless to say, this doesn’t work very well, because the actor can rotate on X+Z Axis, but the camera remains at a static angle, so in the end I’ve decided to ask here, I’m stumped! and some help would be appreciated, this is all in preparation for what I am really trying to do, and that is A-Star pathing, but I am very unfamiliar with Unity, and I’m sure there has to be an easier way to do this.
Thanks in advance, looking forward to hearing some other strategy…
For determining if anything is blocking your camera, raycast from the player backwards to where you want the camera to be. You’ll have to have the angles you want the camera to be [based on user input or whatever but you should always know where you want the camera to go]. Then cast a ray starting from the player and going towards this “desired” camera location. The first thing you hit tells you how far away the camera will be [put it a little closer than this position to the player]. If it is too close to the player you’ll have to make some more code to somehow smoothly get out of the way [going left/right or up/down or some combo of those].
Thank you Spanky555, so lets say I want the camera directly behind the player as far as it can go, in this case the player is a prefab, how would I cast the ray, that reads the Vector3.zero direction? (In my case you are in a randomly generated 3D maze - so the camera can’t go further than about 60 units anways.)
Thanks for your time, I seem to have it figured out now…
I think of another solution for this, that might also work, just want to throw it in to suggest something new
I would (if possible) attach an collider to the camera and set it to trigger mode, you could then check if the camera collides with your other objects, and reduce it’s distance from player a little bit and rechecking, until it doesn’t collide with anything. Might be a bit easier to code, but I guess it’s not the best way to do it. Well, at least it could work and it’s easy