Okay, so I have a camera I’ve been building for a 2D sidescrolling game. The player needs to stay slightly off to the left - like an original Gradius game:
The camera moves freely from the player, and needs to stay that way. At the moment, I just can’t get the offset working right - I keep trying the various screen conversions but I’m not sure what will work - here is my code:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public Transform TrackTarget;
public float dampTime = 0.15f;
public float OffsetFloat = 0.05f;
public bool Ready = false;
private Vector3 velocity = Vector3.zero;
private Vector3 TargetOffset = Vector3.zero;
void Start()
{
TargetOffset = (camera.WorldToViewportPoint(TrackTarget.position * OffsetFloat));
Vector3 point = TrackTarget.position + TargetOffset;
Vector3 Destination = new Vector3(point.x, point.y, transform.position.z);
transform.position = Destination;
}
void FixedUpdate () {
TargetOffset = (camera.WorldToViewportPoint(TrackTarget.position * OffsetFloat));
if (Ready) {
if (TrackTarget)
{
Vector3 point = TrackTarget.position + TargetOffset;
Vector3 Destination = new Vector3(point.x, point.y, transform.position.z);
transform.position = Vector3.SmoothDamp(transform.position, Destination, ref velocity, dampTime);
}
}
}
void LateUpdate()
{
if (Ready) {
Vector3 CameraPosition = transform.position;
CameraPosition.z = -10.00f;
transform.position = CameraPosition;
}
}
}