I want to make a script so that the camera will follow the player. I need a variable for X amount. Then, if the player is moving right, it will be -Xamount. Then if the player is moving left, it will be +Xamount. So the player will always be off center of the screen. That way he can see what is in front of him. I also want the damping involved to make a smooth transition. This is what I have but Its not working properly.
using UnityEngine;
using System.Collections;
public class SmoothCamV2 : MonoBehaviour
{
public Transform target;
public float zDistance = 3.0f;
public float height = 3.0f;
public float damping = 5.0f;
public float xDistance;
private float dist = 0;
public bool followBehind = true;
public bool facingRight = true;
void Awake()
{
var t = GameObject.Find("Player2D");
target = t.gameObject.transform;
}
void FixedUpdate()
{
float dir = CrossPlatformInput.GetAxis("Horizontal");
if (dir < 0) { dist = -xDistance; }
if (dir > 0) { dist = xDistance; }
}
void Update()
{
Vector3 wantedPosition;
if (followBehind)
wantedPosition = target.TransformPoint(dist, height, -zDistance);
else
wantedPosition = target.TransformPoint(dist, height, -zDistance);
transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);
}
}