playMaker

Author Topic: 2D Camera Follow  (Read 9404 times)

Stanimation

  • Playmaker Newbie
  • *
  • Posts: 5
2D Camera Follow
« on: October 28, 2012, 03:26:13 PM »
Hey All! First post for me.(Actually 2nd, i posted same question on ios forum) I'm an extreme newbie. So apologies if this has been addressed. I'm doing a side-scroller 2d type of game. I cant figure out how to have the camera follow the animating object, as the object approches the edge of the field of view. As an example, imagine a stick-man running left or right. As stick-man gets close to the egde of frame, the camera starts to follow. If the stick-man reverses direction, the camera stops and waits until it gets close to the other edge before following again. Seems easy enough! Ha! I have been battling this for days. Currently i just have the camera parented to the object. But as you can imagine, that sucks. Thanks for any and all help on this one.

VectorF22

  • Junior Playmaker
  • **
  • Posts: 50
Re: 2D Camera Follow
« Reply #1 on: October 29, 2012, 12:30:50 PM »
Hi,

If you want a quick and easy possible solution, then don't use playmaker. Import the Unity Standard Assets (Mobile) Package and use the 'Smooth Follow 2D' script that's in there. You just whack that on your camera and choose a game object for it to follow!

I'm trying to work out an effective way of being able to do the same thing using playmaker in this forum topic...
http://hutonggames.com/playmakerforum/index.php?topic=2479.0

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: 2D Camera Follow
« Reply #2 on: October 29, 2012, 12:33:16 PM »
let's approach this systematically:

1) we need a way to detect whenever the character reaches the edge of the view
and
2) we need to change the cameras x position to fit.

So how could we see if the character reaches the end of the screen? There're a couple of ways, but the easiest in a 2D game would just be a float called "max delta position". We don't need a min for now.
So what you do to get this float is move your character from the centre of the view to the edge of the view. Note the X position (if X is left/right) in world space (just unparent the character). Then undo it and note the normal X position. Subtract your first note from your second, and you'll have the total amount of space between the center of the screen and the side of it.

Now, make sure the camera is separate from the character. THen let's start the FSM on the camera:

First we want to get the x position of the character and the x position of the camera, both in world space. Then we'll use the camera position and make it max the "camera position" to "character position - max delta position" (remember, the one in the previous paragraph). Now, if the camera position is less than character position - max delta position, then it'll be made to that value. If we now assign the new camera X position in a set position action to the camera, it'll always move with the player once he reaches the right side of the screen.
The same needs to be done for the other side of the screen, and you do it by using min in the float operator and by using "character position + max delta position" as the min Value. Now of course these + and - terms need separate float operators.

Now, if you do all this each frame, your camera will follow the character as soon as he reaches the edge of the screen.
I hope it helped, and do note that I did this all in my head, so some details may be mangled, but the general principle should work ;)

Cheer,
kiriri
Best,
Sven

Stanimation

  • Playmaker Newbie
  • *
  • Posts: 5
Re: 2D Camera Follow
« Reply #3 on: October 29, 2012, 11:33:11 PM »
@VectorF22 Jeez!! Sometimes the solutions is right in front of you. Not a perfect solution but close enough. Thanks man!

@kiriri  Thanks for the help on this one. I'm gonna try it just to see how it works. Thanks for the help!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: 2D Camera Follow
« Reply #4 on: October 30, 2012, 03:35:06 AM »
Hi,

 I have implemented something like that in the platform jumper from M2H:

it follow vertically the player only when it reaches a certain height. You can apply the same technic when the user is moving side ways.

https://hutonggames.fogbugz.com/default.asp?W890

bye,

 Jean