Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

ChangableLookAt

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

Thread Status:
Not open for further replies.
  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.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,357
    This thread is nearly 20 years old!!!

    Please don't necro-post. If you have a new question, make a new post. It's FREE!!




    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?



    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    - Do not TALK about code without posting it.
    - Do NOT post unformatted code.
    - Do NOT retype code. Use copy/paste properly using code tags.
    - Do NOT post screenshots of code.
    - Do NOT post photographs of code.
    - Do NOT attach entire scripts to your post.
    - ONLY post the relevant code, and then refer to it in your discussion.
     
Thread Status:
Not open for further replies.