SmoothFollow2D: Change camera position ?

Hello,
I have been looking at this script for hours and I still can’t understand it. If I change the rotation of the camera in the editor the camera stays that way, BUT if I change the position of the camera and hit play the camera goes back to default position (Off course). But I still can’t find a simple way to change lower the camera! Please help me find a solution. (Yes, I’m a noob!)

Best regards,

Joar

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);
	thisTransform.position.y = Mathf.SmoothDamp( thisTransform.position.y, 
		target.position.y, velocity.y, smoothTime);
}

Where did you even get this script? It cannot work, as you cannot directly modify transform.position.x or transform.position.y. If you want to lower the camera, replace everything in there with:

function Start()
{
	transform.position = new Vector3(0f, -1f, 0f);
}

That will lower your camera 1 unit on the Y axis. Change the -1f to however much you wish to lower your camera by.

Thanks for the reply! I got it from the file SmoothFollow2D, the penelope iphone-project. It’s attached to the maincamera.

But can’t I combine your code with the SmoothFollow2D-script? If I remove everything I guess the camera won’t follow the “target”?

Oh, you want your camera to FOLLOW the target? My apologies, I did not understand what you wanted.

If you want the camera to follow the target, then simply do something like this:

public Transform target;
private Vector3 currentTarget;
private Vector3 followVel;

// Use this for initialization

void Start () {
	followVel = Vector3.zero;
	currentTarget = target.position;
}

// Update is called once per frame

void Update () {
	if (target == null)
		return;

	currentTarget = Vector3.SmoothDamp(currentTarget, target.position + new Vector3(0f,0f,-5f), ref followVel, .5f);
	transform.position = currentTarget;
}

Ideally you would put this code onto your Main Camera, and then drag into the slot “target” whatever object you want the camera to track–this is assuming you are making a 2D game and your camera is from the side! This is why the Vector3 has a -5f in the Z axis, it’s to pull the camera back by 1 unit (which you can change, based on however much you would like to see).

I really appreciate your help but the script I already got works great, I just want to lower the camera. Can’t I somehow add the: (0f,0f,-5f) snippet in the code I originally posted?

I think I found a solution. I simply added -1 directly after velocity.y. Please let me know if there is a better solution!

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);
	thisTransform.position.y = Mathf.SmoothDamp( thisTransform.position.y, 
		target.position.y, velocity.y, -1, smoothTime);
}

Hmm… could we add a little trickery in here. Create a target empty object on your scene, and have it use stay z out from your model at all times. Then, your target is a fixed point looking at your character.

I am guessing that you don’t want to rotate your camera at all (i.e. look at your player) so that should work just fine.

Lastly, in the script for the target, have it stay on the X and Y, but you could also have it move outward based on the velocity that he is moving. This would have the effect of zooming out when he is moving.

var player : Transform;
var minDistance : float = 20.0;
var velocityModifier : float =  1.0;
var lastPosition : Vector3;

function Start(){
	lastPosition=player.position;
}

function Update(){
	this.transform.x=player.position.x
	this.transform.y=player.position.y
	
	var velocity=Vector3.Distance(player.position, lastPosition);
	this.transform.z=player.position.z - minDistance - velocity * velocityModifier; 
}

Thank you so much for your reply. I tried adding the script you posted but I got three errors, one for each angle:
Assets/NewBehaviourScript.js(11,24): BCE0019: ‘x’ is not a member of ‘UnityEngine.Transform’.
Assets/NewBehaviourScript.js(11,24): BCE0019: ‘y’ is not a member of ‘UnityEngine.Transform’.
Assets/NewBehaviourScript.js(11,24): BCE0019: ‘z’ is not a member of ‘UnityEngine.Transform’.

How do I do this?

(By the way, the -1 solution I mentioned earlier removes the ability to make the camera follow the player if he jump or moves in upwards.)

here is your fix:
just add a rigidbody to your cam and turn of the gravity

#pragma strict
var target : Transform;
var smoothTime = 0.3;

private var velocity : Vector2;


function Start () {
    
}

function Update () {
    this.rigidbody.position.x = Mathf.SmoothDamp(this.rigidbody.position.x, 
        target.position.x, velocity.x, smoothTime);

    this.rigidbody.position.y = Mathf.SmoothDamp(this.rigidbody.position.y, 
        target.position.y, velocity.y, smoothTime);

}