Make the camera follow the sprite

Started by
5 comments, last by phil_t 8 years, 11 months ago

Hi everybody :D
I'm currently developing a simple 2D game using Unity, and I need to follow a sprite with the camera. The problem is that the user has to interact with the screen, so I use a Screen coords -> World coords conversion, so I can't just parent the camera to the object or apply to the camera's transform the X and Y coords of the sprite. I was thinking about a script that moved the camera only if the sprite is near its bounds, but I actually don't know how to do it.

So, do you know any efficient way to do this? Any help would be really appreciated :)

Advertisement


I use a Screen coords -> World coords conversion, so I can't just parent the camera to the object

Why can't you? happy.png The conversion of screen coords to world coords should use the camera's view matrix (which includes the camera's position). If not, your conversion is incomplete or incorrect.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.


I use a Screen coords -> World coords conversion, so I can't just parent the camera to the object

Why can't you? happy.png The conversion of screen coords to world coords should use the camera's view matrix (which includes the camera's position). If not, your conversion is incomplete or incorrect.

The problem is that if I parented the camera to the object I want to follow, the two would have the exact same coordinates, and since I have to draw a line with the mouse on screen (which is converted to a physical line in the game's world by using this Screen -> World conversion), this line would also move with the camera, so my followed sprite would never get to touch it, which should have happened.


this line would also move with the camera

Why? If you define the line in world coordinates, it will be displayed (or should if your world/view/projection is setup correctly) where it is in the world.

Your thread question asked about making the camera follow a sprite. You can do that.

However, you then mention problems that may arise when you draw a line. For you to get help solving a problem, it would help others help you if you explained what you want to do, rather than how you want to do it. And describe the entire situation. There are only a few members here on gamedev that can read minds and anticipate something you haven't mentioned. wink.png

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

So, I'm making a game like this one (

), where you have to draw a line to prevent this sprite (in my case a ball) from falling. I'm doing it by getting the start point of your mouse on the screen, then change the end point of this line until you stop pressing the mouse button. Then, I take this coordinates (which are screen ones) and I convert them to world coordinates, to actually draw this line. I apply this points to the renderer and the collider, with a specific physic material. The ball is a rigidbody with a circle collider, and when it touches the line, it bounces on it and it destroys the line, by setting startPoint = endPoint.

To make this ball go up and make the user interact with it until it falls, I should move the camera in order to make it still when I draw a line (if it moved, the end position would move accordingly, making the line almost impossible to be drawn correctly by the user), but I should only move the camera when the ball is actually going out of it. To simplify a lot my problem, I'd like the camera to move only when actually needed, so when the ball goes out of it.

I hope it's everything clear now. In case, I could give you the full Unity project if I explained myself badly :)

Looking at that video, it keeps the line in screen space and simply converts the coords to world space while the line itself remains locked to the camera position.

If you have some differences in your implementation that make it more of a problem then just pin the camera while the mouse button is pressed.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Instead of waiting until the mouse up and converting two screen coordinates at that point, convert the first mouse press to world coordinates right away.

Like the previous poster said though, that is NOT what's happening in the YouTube video.

This topic is closed to new replies.

Advertisement