Question by
gingiethenappingcat · Mar 08, 2018 at 09:16 PM ·
cameraunity 2d
Issue with setting Camera Location
Hello, I have a small problem with using the camera in a 2D environment. I wrote a simple script to set it's location to the average of two points (players) but the camera simply shows white. The script compiles perfectly fine, and has no errors, so what is the problem?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamMov : MonoBehaviour {
public GameObject player1;
public GameObject player2;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.position = player1.transform.position + player2.transform.position / 2;
if (transform.position.z == 0)
}
}
Comment
set it's location to the average of two points (players)
That's not what your code does.
void Update () {
transform.position = (player1.transform.position + player2.transform.position) / 2f;
}
Answer by Cuttlas-U · Mar 09, 2018 at 11:25 PM
hey to find the middle of two position use this code :
transform.position = Mathf.lerp ( player1.transform.position , player2.transform.position , 0.5f) ;
Your answer
![](https://koobas.hobune.stream/wayback/20220612154209im_/https://answers.unity.com/themes/thub/images/avi.jpg)