- Home /
Trouble making a camera transition
I'm trying to change a cameras position by pressing a button and making the camera a child of the selected gameobject and moving to the spot.
the problem im having is im trying to write more code to make a smooth transition from the original camera position to the new camera spot. Whenever I try to make the camera transition it makes the whole software freeze and crash
if anyone is willing to help or give some advice I would greatly appreciate it!
the code below is the code I have underneath the camera script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Camera_Change : MonoBehaviour { bool maincampos;
private Vector3 maincamerapos;
public GameObject custompos;
private Vector3 transition_camera;
public float camera_change_speed;
private void Start()
{
maincamerapos = new Vector3(0, 0, -10);
maincampos = true;
}
private void Update()
{
//allows attachdog() to be called
if (maincampos == true)
{
attachtodog();
}
//allows detatchdog() to be called
else if (maincampos == false)
{
detatchdog();
}
}
//when button is pressed the camera will become a child of the selected gameobject
void attachtodog()
{
if (Input.GetKeyDown(KeyCode.Space))
{
gameObject.transform.parent = custompos.transform;
Debug.Log(gameObject.transform.parent);
maincampos = false;
//camerchange_totarget();
camera_transition();
}
}
//when button is pressed the camera will no longer be a child of the selected gameobject
void detatchdog()
{
if (Input.GetKeyDown(KeyCode.Space))
{
gameObject.transform.parent = null;
Debug.Log(gameObject.transform.parent);
maincampos = true;
camerchange_tostart();
}
}
//makes the camera move in a smooth transition to the selected game object
void camera_transition()
{
if(maincamerapos.x < custompos.transform.position.x)
{
maincamerapos = transition_camera;
while(transition_camera.x < custompos.transform.position.x)
{
transition_camera.x = transition_camera.x + camera_change_speed;
}
}
/*if (maincamerapos.x > custompos.transform.position.x)
{
while (maincamerapos.x > custompos.transform.position.x)
{
maincamerapos.x = maincamerapos.x - camera_change_speed;
}
}*/
}
//changes the cameras transform to the gameobject
/*void camerchange_totarget()
{
transform.position = new Vector3(custompos.transform.position.x + 3, custompos.transform.position.y, -10);
}*/
//changes the cameras transform to the original position
void camerchange_tostart()
{
transform.position = new Vector3(0, 0, -10);
}
}
I fixed your code, I hope this is what you wanted to get.
Explanatory video - https://www.youtube.com/watch?v=Ll3dRImXrog
using UnityEngine;
public class Test0 : $$anonymous$$onoBehaviour
{
public float cameraChangeSpeed = 2;
public Transform custompos;
Vector3 maincamerapos, transitionCamera;
Transform tr;
bool maincampos;
void Start()
{
tr = transform;
maincamerapos = tr.position;
}
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
if (maincampos == false)
{
maincampos = true;
tr.parent = custompos;
}
else
{
maincampos = false;
tr.parent = null;
}
Debug.Log(tr.parent);
}
if (maincampos == true) transitionCamera = custompos.position;
else transitionCamera = maincamerapos;
if (tr.position != transitionCamera) tr.position = Vector3.$$anonymous$$oveTowards(tr.position, transitionCamera, Time.deltaTime * cameraChangeSpeed);
}
}
Your answer
Follow this Question
Related Questions
camera follow obect on pan to z axis 0 Answers
Move camera when mouse is near the edges of the screen 1 Answer
How to rotate camera around an object to a certain amount of degrees? 2 Answers
Need help updating camera position to go above the player 2 Answers
Using lerp to go between 3 different locations isn't working 0 Answers