I'm new in Unity and want to create a camera-window like on this website:
http://gamasutra.com/blogs/ItayKeren/20150511/243083/Scroll_Back_The_Theory_and_Practice_of_Cameras_in_SideScrollers.php#h.elfjc4ap4hpe There is a example with Curb Camera Motion. I want to make a camera-window which pushes the camera position as the player hits the window edge.
Any ideas, how to realize this?
I used this code:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public GameObject player;
public Vector3 min;
public Vector3 max;
private Vector3 offset;
void Start ()
{
offset = transform.position - player.transform.position;
}
void LateUpdate ()
{
Vector3 newPos = player.transform.position + offset;
newPos.x = Mathf.Clamp(x, min.x, max.x);
newPos.y = Mathf.Clamp(x, min.y, max.y);
newPos.z = Mathf.Clamp(x, min.z, max.z);
transform.position = newPos;
}
}
Unfortunately, the camera is moving not correct. Any ideas, how to create a camera-window?
The main issue here is that you need to check your targets position in screen space. Where the object is in screen coordinates. And then move your camera if the target is out of the window in "screen coordinates". The main function to be used here is
Camera.main.WorldToScreenPoint
Following is a basic class that should mimic the effect in the article. This can be improved alot but should be enough to get you started in the right direction.
using UnityEngine;
using System.Collections;
public class CurbCam : MonoBehaviour
{
public Transform targetPosition;
public float camWindowDimension;
Vector2 targetScreenPos;
float deltaX;
float deltaZ;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
//convert target pos to 2D
targetScreenPos = Camera.main.WorldToScreenPoint (targetPosition.position);
if (targetScreenPos.x > (Screen.width/2) + camWindowDimension)
{
deltaX = targetScreenPos.x - ((Screen.width/2) + camWindowDimension);
transform.position = new Vector3(transform.position.x + deltaX, transform.position.y, transform.position.z);
}
if (targetScreenPos.x < (Screen.width/2) - camWindowDimension)
{
deltaX = targetScreenPos.x - ((Screen.width/2) - camWindowDimension);
transform.position = new Vector3(transform.position.x + deltaX, transform.position.y, transform.position.z);
}
if (targetScreenPos.y > (Screen.height/2) + camWindowDimension)
{
deltaZ = targetScreenPos.y - ((Screen.height/2) + camWindowDimension);
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + deltaZ);
}
if (targetScreenPos.y < (Screen.height/2) - camWindowDimension)
{
deltaZ = targetScreenPos.y - ((Screen.height/2) - camWindowDimension);
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + deltaZ);
}
}
}
Clamp The Vector3
before applying it to transform.position
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public GameObject player;
public Vector3 min;
public Vector3 max;
private Vector3 offset;
void Start ()
{
offset = transform.position - player.transform.position;
}
void LateUpdate ()
{
Vector3 newPos = player.transform.position + offset;
newPos.x = Mathf.Clamp(x, min.x, max.x);
newPos.y = Mathf.Clamp(x, min.y, max.y);
newPos.z = Mathf.Clamp(x, min.z, max.z);
transform.position = newPos;
}
}
Maybe is not the best approach but you can put 2 colliders at the edges of your Camera and if the player collide with one of them then the Camera will move in the player direction.
In other way you need to calculate the cámera position and the player position both of them referenced at the same point, the center of your scene for example; lets say that your Camera wide is 10 units and for the player are 2 units and both start position is at (0,0).
So, my Camera edges are at (-5,0) & (5,0); and for my player are at (-1,0) & (1,0). Then I need to calculate at every frame the posiiton of my Camera and the player in order to know when the player reach my left or right corner.
Lets say that the Player moves to right until their position is (4,0) it means that his right corner are at (5,0) so, I need to start moving the Camera if the diference betwen distances are higher than certain value (4 in this case because we are walking forward) and the player keeps moving in that direction.
The only thing to take care of is the distance betwen the center of the cámera and the center of the player. If the diference is > to certain number, or lower if you moves backward, then just move the Camera.
Basically is the distance betwen 2 points.