k so 2 details, game is 2D bird’s eye view shooter, and the code below is unfinished.
Basically wat i want to do is have my player always in the center of the screen unless i go near the edge of the screen(similar to as in Mario when u try to go to the left side of the screen the camera doesnt follow u anymore unitl u get back to the center of the screen)
for that, all i do is remove the camera object from the parent object into its own space showing in the code below. this code work but only when i got to the right side of the screen for some reason.(i know i havent coded for the y axes but if i do then it doesnt work at all)
any help?
also as u can see im using fixed number to say where the restrictions starts which wont work if u change resolution of the game so i need a fix for that too, i tried using screen.width and height but also didnt work.
function LateUpdate () {
transform.position = new Vector3(Mathf.Clamp(transform.position.x, -27, 27), Mathf.Clamp(transform.position.y, -36, 36), transform.position.z);
if(player.transform.position.x <= Mathf.Clamp(transform.position.x, -27, 27))
{
transform.parent = player.transform;
}
else
{
transform.parent = null;
}
}
I would suggest not parenting the camera at all, just set the transform.position of the camera to follow the position of the character. Doing this has much more predictable results and isn't necessarily reasonably more expensive to do. Also, take a look at the free Unity Lerpz platformer example, it has a pretty standard smooth follow camera script that you can create your basis off of. http://unity3d.com/support/resources/tutorials/3d-platform-game.html
– dannyskimok late reply but i checked the Lerpz game and the code seems rather complicated + since it deals with changing camera restrictions and changing players it makes it even harder to understand. i tired fixing some things but im very much stuck right now cause there's more problems with the way im doing things as well. i dont usually ask for that much but i dont rly know where i should start, will try look more into that other game but dunno if im gonna gonna make sense of it much; but essentially the behavior of my camera is basically the same as the #D platform tutorial game
– leonalchemist