x


remove damping smoothfollow2d

I'm making an auto 2D-sidescroller like Bit Trip Runner or Canabalt, and I'm using the smooth follow script.

The thing is that the smooth effect/damping make it looks like my character is placed to right. Becuase he's always running the smooth effect is unnecessary. So my question is:

How can I remove the smooth follow effect and just make the camera follow the character?

Here's the script:

var target : Transform;
var smoothTime = 0.3;
private var thisTransform : Transform;
private var velocity : Vector2;

function Start()
{
    thisTransform = transform;
}

function Update() 
{
    thisTransform.position.x = Mathf.SmoothDamp( thisTransform.position.x, 
       target.position.x, velocity.x, smoothTime);
}
more ▼

asked Aug 23 '11 at 08:40 PM

spanjoar gravatar image

spanjoar
31 6 6 8

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

If you want the camera locked to the player's position on the x axis and y axis (assuming your 2 dimensions are x and y) then you'de use a much simpler script.

var target : Transform;
var distance : float;

function Update () {
    transform.position = target.position - transform.forward * distance;
}

That puts the player always right in the center of the screen with the camera backed up to distance.

Let's say you wanted the camera to be centered just above the player, or just in front of the player.

var target : Transform;
var offset : Vector3;

function Update () {
    transform.position = target.position - offset;
}

Now in the inspector you can change the x & y values for offset to put the center of the camera wherever you want and the z value to move the camera forwards or backwards (negative will zoom out.)

more ▼

answered Aug 23 '11 at 08:50 PM

Rennat gravatar image

Rennat
664 5 8 18

Thanks for your fast reply. But I just want the camera to follow the player on the x axis.

When I applied your first script something went wrong with the camera distance and I can't even see the character when I hit "Play". The distance are not effected when I move the camera. Any clue how to solve this?

Aug 23 '11 at 09:02 PM spanjoar

Just to confirm you do have your scene laid out using y as vertical and x as horizontal right?

A variation that would lock the y would be similar but since we're changing all values in a different way we'll just make a new Vector3

var target : Transform;
var distance : float;
var height : float;

function Update () {
    transform.position = new Vector3(target.position.x,
                                     height,
                                     target.position.z - distance);
}
Aug 23 '11 at 09:11 PM Rennat

That's exatcly what I was looking for, thank you so much!

And yes: y = Vertical and x = horizontal.

I didn't notice the distance controler at first, that explain why the camera was zoomed in :)

Aug 23 '11 at 09:31 PM spanjoar

Awesome glad it helped, please "thumbs up" it and check it off as a correct answer :)

Aug 23 '11 at 09:35 PM Rennat
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x93
x50
x15

asked: Aug 23 '11 at 08:40 PM

Seen: 597 times

Last Updated: Aug 23 '11 at 09:35 PM