Hi. Im new to programming and i want to make a cameralock like they use in mario (When not at a certain height locked to y = 0 but when the player reaches a certain y-value the camera smoothly locks to the player.). I´ve tried a every idea i had, like adding 1 to camera y-position until it reaches player y, but nothing works. I Would really appreciate the help.
I don’t know what mario looks like, but it sounds like you want mario to be able to move around in the FOV without being locked in the center of the screen (i.e., the camera always pointing directly at him), but have the camera move to keep him there when he gets close to an edge? I have a couple of space games (3d) where I do the same thing. Keep a direction to the object (mario). Use the camera WorldtoScreen function to get the screen coordinates of mario in the range of 0-1. Get the absolute value of the distance between mario and the screen coordinates of the allowable Y extent, and put that in the range of zero to 1. (y = Mathf.abs(mario.screeny-.5)/0.5. Now, what you can do is lerp the camera’s forward vector toward mario as a function of the number you just calculated. lerpspeed = MathF.Pow( y,somevalue); 3 works good for me as somevalue. What that last part does, the exponentiating, is it makes the value of lerpspeed grow very slowly. If mario is at the edge of your allowable Y, then the value of y you calculate first will be very close to 1, and when you exponentiate it, you’ll get something very close to 1 (1^X = 1) and your camera will follow Mario precisely. As he moves away from that allowable Y value, the y you calculate will be smaller (e.g., .25), and when you exponentiate it it will get even smaller (,.0156) making your camera hardly move at all.
Edit: you can do the same thing in the world coordinates, just get the distance between mario and your Y barrier and put it into the range of 0-1, then exponentiate it, and use that as the lerpspeed and lerp your camera.transform.forward towards mario using that lerpspeed.