I have two objects that move independently to each other, and I would like the camera to move with them, at a midpoint between the positions of the object. I am relatively new to unity and to do this is think I would need to get the positions of the objects from a script in the camera. If anyone can help me with the code to find the vector 3 positions of the objects/the xyz co-ordinates I would be very grateful. ![]()
transform.position?
You should try to take the xyz axies values with transform.position.x, transform.position.y, transform.position.z.
With these values, you can calculate the axies of the new position of the Camera…
So the hard part is getting a reference to the two objects. Does everything already exist on the scene? If so, you can attach a script like this to your camera.
using UnityEngine;
public class MoveToCenter : MonoBehaviour {
// Assign these to your two objects in the Inspector.
public Transform objectA;
public Transform objectB;
void LateUpdate() {
transform.position = 0.5f * (objectA.position + objectB.position);
}
}