Camera Smooth follow gone wrong

Hi guys!

I guess the title explains it all.

I am not so good at scripting so I am very much like at a newbie level just so you know.
I have been following a tutorial about how to script smooth follow for camera but that was written in C# and I want it in JavaScript since I am working with a 2d web game.

So I wrote almost exactly as he wrote it but in javascript, but I am getting errors and I am not so sure what could cause these errors, I’ve already googled but couldn’t find any solution for this, so that is why I’ve turned to you guys.

So here’s my script

To be more specific where the errors occurs is where I wrote "CheckXMargin : Boolean (). But shouldn’t I get the same erors for the other Boolean () too?

Any help would be very much appreciated!
Thanks in advance guys! 8)

OK, let me first start off by saying that SmoothFollow is one of the simplest things you can do, as a matter of fact in JS is is a one liner. :wink:

#pragma strict

public var target: Transform;
public var smoothness: float = 3.0;

function Start () {
	
}

function Update () {
	if(!target){ return; }
	transform.position = Vector3.Lerp(transform.position, target.position, smoothness * Time.deltaTime);
}

What you are trying to do is just a little more complex. You want to limit the distance from the character.

#pragma strict

public var target: Transform;
public var smoothness: float = 3.0;
public var maxDistance: float = 5.0;

function Start () {
	
}

function Update () {
	if(!target){ return; }
	transform.position = Vector3.Lerp(transform.position, target.position, smoothness * Time.deltaTime);
	
	if(Vector3.Distance(transform.position, target.position) > maxDistance)
		transform.position = target.position + (transform.position - target.position).normalized * maxDistance;
}

Lastly, if you want a minimum distance added, to prevent it from clipping or colliding with the object, you would simply add a few more lines of code.

#pragma strict

public var target: Transform;
public var smoothness: float = 3.0;
public var minDistance: float = 1.0;
public var maxDistance: float = 5.0;

function Start () {
	
}

function Update () {
	if(!target){ return; }
	
	if(Vector3.Distance(transform.position, target.position) < minDistance){
		transform.position = target.position + (transform.position - target.position).normalized * minDistance;
		return; // get out if we are too close, this prevents lerping it closer.
	}
	
	transform.position = Vector3.Lerp(transform.position, target.position, smoothness * Time.deltaTime);
	
	
	if(Vector3.Distance(transform.position, target.position) > maxDistance)
		transform.position = target.position + (transform.position - target.position).normalized * maxDistance;
}

Now, if you wanted to add a constant offset, such as on the Z plane, you will need to add something to do that. (Note, you dont need the min distance for this.)

#pragma strict

public var target: Transform;
public var smoothness: float = 3.0;
public var maxDistance: float = 5.0;
public var zOffset : float = -10.0;

function Start () {
	
}

function Update () {
	if(!target){ return; }
	
	transform.position = new Vector3(transform.position.x, transform.position.y, target.position.z);
	
	transform.position = Vector3.Lerp(transform.position, target.position, smoothness * Time.deltaTime);
	
	if(Vector3.Distance(transform.position, target.position) > maxDistance)
		transform.position = target.position + (transform.position - target.position).normalized * maxDistance;
	
	transform.position = new Vector3(transform.position.x, transform.position.y, zOffset);
}

By the way… You are not limited to JavaScript for Web Apps, you may use any language that Unity supports.

Haha! It works, thanks a lot!

Yeah, I might have expressed myself in a wrong way. I knew that you can write in any language when it comes to web games, but my teacher, that I had in my web course told me javascript is the best when it comes to web games, but I am not so sure how experienced he is on that subject when it comes to web games.

I am quite new to unity script and I started just a couple of days ago so I didn’t know it was that easy. But hey, what do you expect of a newbie like me? :stuck_out_tongue:

anyway, cheers mate!
Thank you for taking your time to help a beginner! :smile: