Hello Guest

Author Topic: 2d player follow like ski safari  (Read 8898 times)

krithik_b007

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 16
    • View Profile
2d player follow like ski safari
« on: July 01, 2013, 08:50:53 am »
Hello

I am new to unity i am basically cocos2d developer . I am trying to make a game which is similar to ski safari.

I am using 2d toolkit.

My problem is i want to follow the player with camera , and camera should zoom when the player jumps . Currently i am zooming by changing the camera's orthographic size. But when we change the camera's orthographic size the player moves in x axis which i don't want , i want player to be static in x axis of the screen .

In my current logic i am moving the player with physics and adding force to rigid body.
And camera is following the player with smooth follow script and i am ray casting from player to terrain to determine the height of the player and correspondingly i am changing the orthographic size of camera . But in this approach i am facing many problems and more over the effect is not similar to ski safari. I want to move the player in between 1/4 to 3/4 in the screen, with proper zooming

I have posted the question in forum as well .
http://forum.unity3d.com/threads/186128-2D-Smooth-Follow-with-zoom-Like-Ski-Safari-amp-Tiny-Wings

If anyone can give me suggestion on how to proceed than it will be great .

Thank you
Krithik 

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: 2d player follow like ski safari
« Reply #1 on: July 01, 2013, 10:35:00 am »
You will need to change orthographic size AND move the camera to compensate for the changed size. You will have to work out the position the player is in the current ortho size (camera.worldtoscreenposition) and then once you change the size, use screen to worldposition to work out where the player will need to be in the new camera, and move your camera appropriately.

I've not really played ski safari so I couldn't tell you how you'd get the same effect, but if you work out the above, your camera + player will be at the correct position, and then you can tweak it to feel more similar.

krithik_b007

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: 2d player follow like ski safari
« Reply #2 on: July 02, 2013, 09:48:49 am »
Thank you for the reply .

I tried almost many things from past 3 weeks :)

Can you give some guidance for these problems

1)Since this game has parallax movement , i want the first 2 layers to zoom out when the player jumps and back layers to be constant (no zoom) . To get this effect whether we need to use 2 cameras ? or is there any alternative. I tried to keep scale of those backgrounds as always one by multiplying the camera zoom factor but it gives lot of flicker .
2)I want to move the player in between 1/4 to 3/4 in the screen, with proper zooming how can we make the camera to follow in y axis when the player goes below 1/4 of the screen & 3/4 of the screen ? since cameras position is dependent on player position i am finding difficulty to get boundary values.

If you can please check out this video  , its the gameplay of ski safari to check out camera follow.
http://www.youtube.com/watch?v=K2gGtDFKB0o

Thank you .

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: 2d player follow like ski safari
« Reply #3 on: July 02, 2013, 10:36:19 am »
1. That video looks very much like they're changing the field of view on a perspective camera. The backgrounds ARE changing in size, just a lot less than the foreground because of the parallax.

2. You will have to position the camera based on the player.
 i. update player position
 ii. decide screen position of player in camera
 iii. calculate world position for screen position in camera
 iv. get delta between world position and camera position.
 v. position camera using delta and player position.

krithik_b007

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: 2d player follow like ski safari
« Reply #4 on: July 02, 2013, 11:12:33 am »
Thank you for the reply .
I will look into all these calculations :)

Currently i am using orthographic camera do i need to use perspective camera because field of view is present in perspective camera ?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: 2d player follow like ski safari
« Reply #5 on: July 02, 2013, 12:08:44 pm »
It would be easier to get the effect with a perspective camera.

krithik_b007

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: 2d player follow like ski safari
« Reply #6 on: July 03, 2013, 12:28:47 pm »
Thanks unikronsoftware.

I was able to solve the change in players X position when zooming just by using small logic :) i.e. multiplying by zoom factor .

float zoomSize = _camera.orthographicSize/384; (384 is minimum orthographic size
newPosition = new Vector3 (player.transform.position.x + 384 * zoomSize, player.transform.position.y, this.transform.position.z);   //here 384 is minimum x distance in x axis.
this.transform.position = newPosition;

:)

Now looking forward to solve the player in between 1/4 to 3/4 of the screen :)