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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

ChangableLookAt

Discussion in 'Editor & General Support' started by Jonathan Czeck, Apr 13, 2005.

  1. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Hey. I'm working on a LookAt camera script that changes when you click on a specific type of object. (In this case a game bal.) I'm having trouble figuring out Tags. When I set one in the editor, it becomes an Invalid Tag. I think I'm missing something.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ChangableLookAt : MonoBehaviour
    6. {
    7.     public Transform lookAt;
    8.     public float smooth = 5F;
    9.  
    10.     Quaternion lastRotation;
    11.     Quaternion goalRotation;
    12.  
    13.     void FixedUpdate ()
    14.     {
    15.         if (lookAt != null)
    16.         {
    17.             Debug.DrawLine(lookAt.position, transform.position, Color.gray);
    18.  
    19.             Vector3 difference = lookAt.position - transform.position;
    20.             Quaternion rotation = Quaternion.LookRotation(difference, Vector3.up);
    21.             goalRotation = rotation;
    22.         }
    23.     }
    24.  
    25.     void Update ()
    26.     {
    27.         if (Input.GetMouseButtonDown(0))
    28.         {
    29.             Ray ray = camera.ScreenPointToRay (Input.mousePosition);
    30.            
    31.             RayCollision coll = Dynamics.CastRay (transform.position, ray.direction);
    32.            
    33.             if (coll != null)
    34.             {
    35.                 GOComponent goc = (GOComponent) coll.collider;
    36.                
    37.                 if (goc.CompareTag("Selectable"))
    38.                 {
    39.                     lookAt = goc.transform;
    40.                 }
    41.             }
    42.         }
    43.     }
    44.  
    45.     void Awake ()
    46.     {
    47.         lastRotation = transform.rotation;
    48.         goalRotation = lastRotation;
    49.     }  
    50.    
    51.     void LateUpdate ()
    52.     {
    53.         lastRotation = Quaternion.Slerp (lastRotation, goalRotation, smooth * Time.deltaTime);
    54.         transform.rotation = lastRotation;
    55.     }
    56. }
    57.  
    -Jon
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You need create the tag in the tag manager first.
    (Edit -> Project Settings -> Tags)

    After you have created the tag you can assign it to a game object.