Camera not switching to characters

I got the code to where the player can push a button to swap characters, but the camera defaults it’s default position upon pushing the button.

here is part of code

camera code is basically smoothfollow 2D

var target1 : Transform; 
var target2 : Transform; 
var smoothTime = 0.3; 
private var thisTransform : Transform; 
private var velocity : Vector2;

function Start() 
	{ 
		thisTransform = transform; 
	}

function Update() 
	{ 
		if (Globalscript.victimstate == 1)
		{
			thisTransform.position.x = Mathf.SmoothDamp( thisTransform.position.x, target1.position.x, velocity.x, smoothTime); 
			thisTransform.position.y = Mathf.SmoothDamp( thisTransform.position.y, target1.position.y, velocity.y, smoothTime); 
		}
		
		if (Globalscript.victimstate == 2)
		{
			thisTransform.position.x = Mathf.SmoothDamp( thisTransform.position.x, target2.position.x, velocity.x, smoothTime); 
			thisTransform.position.y = Mathf.SmoothDamp( thisTransform.position.y, target2.position.y, velocity.y, smoothTime); 
		}
	}

and here is code to swap states

if (Input.GetButtonDown("Switch"))
{
	
	Globalscript.victimstate = 2;
	print(Globalscript.victimstate);
	var spawnplayer = Instantiate(nextCharacter, transform.position, transform.rotation);
	Destroy(gameObject);
}

The state does changes but the camera isn’t following new character.The character does change.

Is the camera attached to the nextCharacter taged as main camera?

Yes it is, would it have anything to d with it being a prefab?

I dont know then, maybe if you post a zip with a simplified version of your project to take a look inside the code.

Here it is, haven’t really touched it up, when you switch the characters the 2nd character falls through the world but the camera should follow it anyways so it gets the point across.

Any help would be great.

738053–26943–$2dcamera.zip (76.1 KB)

here is you script modified , at least switch will work , now i don’t know why you destroy your initial target cause this is going to be extremely not practical when you will want switch back on it if that was your intention…

for now i just did in a way you get onto your second target…

here i put what modified form your script >

// put your global script in scene on empty object...and check input from it , it is more logical to me to 
// send infos that way considering the your global script act more or less as your game controller / manager
var victimstate : int = 1;
var nextCharacter : Transform;

function Update () 
{
	if(Input.GetButtonDown("Switch"))
	{
		if(victimstate == 1)
		{
			var spawnplayer = Instantiate(nextCharacter, transform.position, transform.rotation);
			Camera.mainCamera.SendMessage("GetNewTarget", spawnplayer, SendMessageOptions.DontRequireReceiver);
			victimstate = 2;
		}
	}
}
// your script twoDD look like that now...

var initialTarget : Transform; 
var smoothTime = 0.3; 
private var currentTarget : Transform; 
private var thisTransform : Transform; 
private var velocity : Vector2;

function Start() 
{ 
	thisTransform = transform;
	currentTarget =  initialTarget;
}

function GetNewTarget(target : Transform)
{
	currentTarget = target;
	
	// look like you want destroy your initial target not sure why ...and that will prevent you to switch back
	// instead disable it components..
	Destroy(initialTarget.gameObject);
}

function Update() 
{ 
		
	thisTransform.position.x = Mathf.SmoothDamp( thisTransform.position.x, currentTarget.position.x, velocity.x, smoothTime); 
	thisTransform.position.y = Mathf.SmoothDamp( thisTransform.position.y, currentTarget.position.y, velocity.y, smoothTime); 
				
}

and your player 1 script

var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;

var nextCharacter : Transform;

private var moveDirection : Vector3 = Vector3.zero;


function Update () 
{
        // cache this in a start fct , no reason you need to look that every frame, waste of cpu ..
	var controller : CharacterController = GetComponent(CharacterController);

	if (controller.isGrounded) 
	{

		//Physics.IgnoreCollision(Bullitprefab.collider, collider);
		// sets up move variables
		moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,0);
		moveDirection *= speed;
	}
			// Apply gravity
		moveDirection.y -= gravity * Time.deltaTime;

		// Move the controller
		controller.Move(moveDirection * Time.deltaTime);
}

you should have a unique player control that fit for every player you add in , imagine you have 100 various player , you are not going to do player1 script…player100 script XD…

well hope that help

738083–26945–$2dcamera 2Modif.zip (195 KB)

thanks but yes, I am going to have the initial character be a prefab(with it’s persistant elements in a globalscript that cannot be destroyed) but for this I just put it as a non-prefab. Will look at it later but thanks alot.

but with the globalscript I feel its amateurish that I put it in a light :frowning:

is there a way you can put a script in a scene without it being attached to something?

As far as I’m aware, no, but you could always create an empty object and attach it to that?

you need object in your scene to work with :)…so no you can’t put script in scene without having an object for it…after you can perfectly have non monobehaviour class and access them too, not sure how it work in unityScript , i am using most only C#.