I have a camera that follows the character, and it works fine in the editor, but when I make the build, the camera doesn’t move at all.
The cod eis below, but overall, the camera has two modes: one that follows the characters, but must be inside a collider, so at the start and end of the level, the character won’t be in the center. The second mode make the camera go from it’s position to the character. This is for the instances where the camera stops for a combat encounter, and then goes back to the character.
The camera has some triggers as children, but I removed them and the camera still doesn’t move when making the build. Again, it works perfectly in the editor. This camera has been copied from another scene, and I think I copied it back, so it isn’t a fresh original camera. I created a new one but again, not moving at all.
I don’t see how the code can be wrong if it works in the editor. I also tested using only one of the camera modes to see if one is broken in some way, but again, works in the editor, no when build. I also don’t think I changed anything important in the build options. Even reseting the build options won’t change a thing.
Anyway, this is the code for the camera:
public Transform player;
public Vector2 margin;
public Vector2 smoothing;
public BoxCollider2D limites;
private Vector3 max;
private Vector3 min;
private Vector3 velocity = Vector3.zero;
private Camera camara;
public bool activo = true;
// Transforms to act as start and end markers for the journey.
public Transform startMarker;
private Vector3 start2;
private Vector3 end2;
public bool goTo;
// Movement speed in units per second.
private float speed = 0.08F;
// Time when the movement started.
private float startTime;
// Total distance between the markers.
private float journeyLength;
// Use this for initialization
void Start () {
max = limites.bounds.max;
min = limites.bounds.min;
camara = GetComponent<Camera>();
player = GameObject.FindWithTag("Player").transform;
// Keep a note of the time the movement started.
startTime = Time.time;
// Calculate the journey length.
startMarker = gameObject.transform;
journeyLength = Vector3.Distance(startMarker.position, player.position);
}
// Update is called once per frame
void Update () {
if(activo)
{
moverse();
}
//prueba2
if(goTo)
{
// Distance moved equals elapsed time times speed..
float distCovered = (Time.time - startTime) * speed;
// Fraction of journey completed equals current distance divided by total distance.
float fractionOfJourney = distCovered / journeyLength;
// Set our position as a fraction of the distance between the markers.
start2 = new Vector3(startMarker.position.x,0,-10);
end2 = new Vector3(player.position.x,0,-10);
transform.position = Vector3.Lerp(start2, end2, fractionOfJourney);
if(transform.position.x == player.position.x) {
goTo = false;
activo = true;
}
}
}
public void moverse()
{
//Debug.Log(player.transform.position);
Vector3 current = transform.position;
float x = transform.position.x;
//float y = transform.position.y;
//if (Mathf.Abs(x - player.position.x) > margin.x)
//{
x = Mathf.Lerp(x, player.position.x, smoothing.x * Time.deltaTime);
//}
//if (Mathf.Abs(y - player.position.y) > margin.y)
//{
//y = Mathf.Lerp(y, player.position.y, smoothing.y * Time.deltaTime); esta linea sola estaba activa
//}
float cameraHalfWidth = camara.orthographicSize * ((float)Screen.width / Screen.height);
x = Mathf.Clamp(x, min.x + cameraHalfWidth, max.x + cameraHalfWidth);
//y = Mathf.Clamp(y, min.y + camara.orthographicSize, max.y + camara.orthographicSize);
//transform.position = new Vector3(x, 0, transform.position.z);
//Vector3 target = new Vector3(0, y, transform.position.z); //esta lina estaba activa
Vector3 target = new Vector3(x, 0, transform.position.z);
//smooth
Vector3 newPos = Vector3.SmoothDamp(current,
target,
ref velocity,
0f);
transform.position = newPos;
}