I have a game that create a clone car in each level, so i can´t put the main camera to target this object because they can change (they can be different game objects)… Someone can give me the simple code to do this?
I already have in c# the script that follow the car, but i can´t define a target because the target change… I just can work with the tag os the target, that in this case is “player”.
Actual script:
using UnityEngine;
using System.Collections;
public class SmoothFollow2D : MonoBehaviour {
private Vector3 velocity = Vector3.zero;
public Transform target;
// Update is called once per frame
void Update ()
{
if (target)
{
Vector3 point = camera.WorldToViewportPoint(target.position);
Vector3 delta = target.position - camera.ViewportToWorldPoint(new Vector3(0.2f, .43f, point.z)); //(new Vector3(0.5, 0.5, point.z));
Vector3 destination = transform.position + delta;
transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, 0);
}
}
}