- Home /
Player in center of screen and camera restriction
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
ok 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
Answer by doomprodigy · Jan 29, 2012 at 01:49 AM
What I would do as dannyskim said is move the transform position of the camera to the character. So On a script that is attached to the player add this:
void Start () {
mainCamera = (GameObject) GameObject.FindWithTag ("MainCamera");
}
void Update () {
mainCamera.transform.position = new Vector3(transform.position.x,10,transform.position.z);
}
And then on a script attached to the main camera add:
transform.position = new Vector3(Mathf.Clamp(transform.position.x, leftLimit, rightLimit), transform.position.y, transform.position.z);
In the Update somewhere, where left limit and right limit are public floats you have defined at the top of the script.
Note that this is done in C# but is very easily converted to Unity Script, if you look at the documentation.
Peace,
thx, after a couple tweaks i kinda made it work.. well it works but it messes up 2 other things. um wat i changed in the update was
mainCamera.transform.position = new Vector3(transform.position.x,transform.position.y,-14);
because im using the x and y to move up and down, just makes more sense to me and on my camera script
transform.position = new Vector3($$anonymous$$athf.Clamp(transform.position.x, -27.2, 27.2), $$anonymous$$athf.Clamp(transform.position.y, -35.7, 35.7), transform.position.z);
because the camera restriction has to work with the y axes as well.
EFIT: removed previous problem cause it was an easy fix
and i have another script that shows the damage on enemies head like an rpg but when i reach the side of the screen, the number kinda go the opposite direction of the player
its either gotta do with those 2 lines i think...
function Start () { GUIDamagePosition = Camera.main.ViewportToWorldPoint(gameObject.transform.position); }
function Update() { //Convert WorldToViewportPoint to a vecotr3 so the GUIText position remains on the enemy transform.position = Camera.main.WorldToViewportPoint(GUIDamagePosition); }
i'll obviously look into it myself but i wouldnt be against some help cause this seems a bit confusing
lol gigantic cooment :/
No problem. I suggest taking the answer as the correct one to declutter the unanswered and if you have any other problems regarding a different part of coding to make a new question so that the rest of the UA community can help answer it for you.