Camera that target gameobject with Tag "Player"

Hello!

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);
        }
      
    }
}

You can do this :

using UnityEngine;
using System.Collections;
public class SmoothFollow2D : MonoBehaviour {

    private Camera camera;
    private Vector3 velocity = Vector3.zero;
    public Transform target;

    void Start()
    {
        camera = GetComponent<Camera>();
        target = GetTargetByTag("Player");
    }

    // 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));
            Vector3 destination = transform.position + delta;
            transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, 0);
        }

    }

    /// <summary>
    /// Changes the target.
    /// </summary>
    /// <param name="_target">_target.</param>
    void ChangeTarget(Transform _target)
    {
        target = _target;
    }

    /// <summary>
    /// Gets the target by tag.
    /// </summary>
    /// <param name="_tag">_tag.</param>
    void GetTargetByTag(string _tag)
    {
        GameObject obj = GameObject.FindWithTag(_tag);
        if(obj)
        {
            ChangeTarget(obj.transform);
        }
        else
        {
            Debug.Log("Cant find object with tag "+ _tag);
        }
    }
       

}

Hope this help.

An error as ocurred: Cannot implicitly convert type void' to UnityEngine.Transform’

You know what´s happen?

Thank you for the help

Oh, sorry, change this line :
target= GetTargetByTag(“Player”);
To this :
GetTargetByTag(“Player”);

1 Like

It works, thanks!

If someone needs i find other solution, just put the code bellow:

else {
                        GameObject go = GameObject.FindGameObjectWithTag ("Player");
            if(go == null)
                return;

            target = go.transform;
                }

Thank you :slight_smile:

Can that target more than one models with the same tag?