Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Camera that target gameobject with Tag "Player"

Discussion in '2D' started by jorgegb1997, May 7, 2015.

  1. jorgegb1997

    jorgegb1997

    Joined:
    Apr 15, 2014
    Posts:
    56
    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:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SmoothFollow2D : MonoBehaviour {
    5.  
    6.     private Vector3 velocity = Vector3.zero;
    7.     public Transform target;
    8.  
    9.     // Update is called once per frame
    10.     void Update ()
    11.     {
    12.         if (target)
    13.         {
    14.             Vector3 point = camera.WorldToViewportPoint(target.position);
    15.             Vector3 delta = target.position - camera.ViewportToWorldPoint(new Vector3(0.2f, .43f, point.z)); //(new Vector3(0.5, 0.5, point.z));
    16.             Vector3 destination = transform.position + delta;
    17.             transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, 0);
    18.         }
    19.      
    20.     }
    21. }
     
  2. psyydack

    psyydack

    Joined:
    Aug 30, 2013
    Posts:
    93
    You can do this :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class SmoothFollow2D : MonoBehaviour {
    4.  
    5.     private Camera camera;
    6.     private Vector3 velocity = Vector3.zero;
    7.     public Transform target;
    8.  
    9.     void Start()
    10.     {
    11.         camera = GetComponent<Camera>();
    12.         target = GetTargetByTag("Player");
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update ()
    17.     {
    18.         if (target)
    19.         {
    20.             Vector3 point = camera.WorldToViewportPoint(target.position);
    21.             Vector3 delta = target.position - camera.ViewportToWorldPoint(new Vector3(0.2f, .43f, point.z));
    22.             Vector3 destination = transform.position + delta;
    23.             transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, 0);
    24.         }
    25.  
    26.     }
    27.  
    28.     /// <summary>
    29.     /// Changes the target.
    30.     /// </summary>
    31.     /// <param name="_target">_target.</param>
    32.     void ChangeTarget(Transform _target)
    33.     {
    34.         target = _target;
    35.     }
    36.  
    37.     /// <summary>
    38.     /// Gets the target by tag.
    39.     /// </summary>
    40.     /// <param name="_tag">_tag.</param>
    41.     void GetTargetByTag(string _tag)
    42.     {
    43.         GameObject obj = GameObject.FindWithTag(_tag);
    44.         if(obj)
    45.         {
    46.             ChangeTarget(obj.transform);
    47.         }
    48.         else
    49.         {
    50.             Debug.Log("Cant find object with tag "+ _tag);
    51.         }
    52.     }
    53.        
    54.  
    55. }
    Hope this help.
     
    Last edited: May 7, 2015
  3. jorgegb1997

    jorgegb1997

    Joined:
    Apr 15, 2014
    Posts:
    56
    An error as ocurred: Cannot implicitly convert type `void' to `UnityEngine.Transform'

    You know what´s happen?

    Thank you for the help
     
  4. psyydack

    psyydack

    Joined:
    Aug 30, 2013
    Posts:
    93
    Oh, sorry, change this line :
    target= GetTargetByTag("Player");
    To this :
    GetTargetByTag("Player");
     
    jorgegb1997 likes this.
  5. jorgegb1997

    jorgegb1997

    Joined:
    Apr 15, 2014
    Posts:
    56
    It works, thanks!

    If someone needs i find other solution, just put the code bellow:
    Code (CSharp):
    1. else {
    2.                         GameObject go = GameObject.FindGameObjectWithTag ("Player");
    3.             if(go == null)
    4.                 return;
    5.  
    6.             target = go.transform;
    7.                 }
    Thank you :)
     
  6. dudedude123

    dudedude123

    Joined:
    Aug 24, 2013
    Posts:
    128
    Can that target more than one models with the same tag?