Search Unity

My 1st project. Help needed to move the camera between multiple objects, with options to select.

Discussion in 'Scripting' started by dineshlancerevo, Dec 18, 2021.

  1. dineshlancerevo

    dineshlancerevo

    Joined:
    Dec 6, 2021
    Posts:
    2
    Hello Seniors,

    Im new to Unity and making my 1st simple project for mobile.

    I have a simple environment with 3 houses.

    How can I :

    1) Show the default camera with OnScreenOptions to select "House1 , 2 & 3 respectively".

    2) Upon selecting the option, the camera should move smoothly to that particular house (or object) with a background voice reading "House 1" or whatever object is selected.

    3) Show it for some 10seconds and

    4)return back to the default camera again (With ONSCREEN OPTIONS to select the houses)

    5) Also if the particular house is already viewed, it should be showed in a half white color in the OnScreenOptions

    Sorry for the nooby question. Im learning unity and it would be helpful if u send me the script or at least by guiding me to achieve this task successfully.

    Attached the Screenshot for better understanding.

    Thanks in Advance.
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Camera stuff is pretty tricky... you may wish to consider using Cinemachine from the Unity Package Manager.
     
  3. Serge_Billault

    Serge_Billault

    Joined:
    Aug 26, 2014
    Posts:
    190
    I made you a functional exemple.

    Link to Unity project and code: https://drive.google.com/file/d/1egh68052XYONHGWCTsr-FLq28LwS7iKu/view?usp=sharing
    Because I know that people starting with Unity have trouble with UI setup, some of the UI creation have been automated, but you will still need to adjust the Point Of Interest of showcased products you duplicate even if the sample exemple will automatically adjust to the number of products.

    products_showcase.jpg

    Code samples:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. //************************************************************************************
    7. //
    8. //************************************************************************************
    9.  
    10. public class ProductSlot : MonoBehaviour
    11. {
    12.     //********************************************************************************
    13.     //
    14.     //********************************************************************************
    15.  
    16.     static public readonly List< ProductSlot > instances = new List< ProductSlot >();
    17.  
    18.     static public void Sort() { instances.Sort( ( ProductSlot lhs, ProductSlot rhs ) => { return lhs.transform.position.x.CompareTo( rhs.transform.position.x ); } ); }
    19.  
    20.     //********************************************************************************
    21.     //
    22.     //********************************************************************************
    23.  
    24.     [ SerializeField ] private Transform m_POI = null;
    25.  
    26.     //********************************************************************************
    27.     //
    28.     //********************************************************************************
    29.  
    30.     public Transform POI { get{ return m_POI; } }
    31.  
    32.     //********************************************************************************
    33.     //
    34.     //********************************************************************************
    35.  
    36.     private void Awake()     { instances.Add   ( this ); }
    37.  
    38.     private void OnDestroy() { instances.Remove( this ); }
    39. }
    40.  
    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. //************************************************************************************
    8. //
    9. //************************************************************************************
    10.  
    11. public class Showcase : MonoBehaviour
    12. {
    13.     //********************************************************************************
    14.     //
    15.     //********************************************************************************
    16.  
    17.     public delegate void FreezeCallback();
    18.  
    19.     //********************************************************************************
    20.     //
    21.     //********************************************************************************
    22.  
    23.     [ SerializeField ] private float          m_showcase_duration = 2.0f;
    24.  
    25.     [ SerializeField ] private Camera         m_cam               = null;
    26.                                                        
    27.     [ SerializeField ] private Transform      m_POI               = null;
    28.                                                        
    29.     [ SerializeField ] private AnimationCurve m_curve             = new AnimationCurve();
    30.                                                        
    31.     [ NonSerialized  ] private Vector3        m_start_pos         = Vector3.zero;
    32.                                                        
    33.     [ NonSerialized  ] private Quaternion     m_start_rot         = Quaternion.identity;
    34.                                                        
    35.     [ NonSerialized  ] private Transform      m_dst               = null;
    36.                                                        
    37.     [ NonSerialized  ] private float          m_transition_start  = 0.0f;
    38.                                                        
    39.     [ NonSerialized  ] private float          m_freeze_start      = 0.0f;
    40.                                                        
    41.     [ NonSerialized  ] private float          m_freeze_duration   = 0.0f;
    42.                                                        
    43.     [ NonSerialized  ] private FreezeCallback m_freeze_cb         = null;
    44.  
    45.     //********************************************************************************
    46.     //
    47.     //********************************************************************************
    48.  
    49.     private bool busy { get { return m_dst != null; } }
    50.  
    51.     //********************************************************************************
    52.     //
    53.     //********************************************************************************
    54.  
    55.     private void Freeze( float duration, FreezeCallback cb )
    56.     {
    57.         if( m_freeze_start <= 0.0f )
    58.         {
    59.             m_freeze_start    = Time.unscaledTime;
    60.  
    61.             m_freeze_duration = duration;
    62.  
    63.             m_freeze_cb       = cb;
    64.         }
    65.     }
    66.  
    67.     //********************************************************************************
    68.     //
    69.     //********************************************************************************
    70.  
    71.     public void Focus( Transform poi )
    72.     {
    73.         if( m_cam == null ) return;
    74.  
    75.         if( busy  == true ) return;
    76.  
    77.         m_dst = poi;
    78.  
    79.         m_start_pos = m_cam.transform.position;
    80.  
    81.         m_start_rot = m_cam.transform.rotation;
    82.  
    83.         m_transition_start = Time.unscaledTime;
    84.     }
    85.  
    86.     //********************************************************************************
    87.     //
    88.     //********************************************************************************
    89.  
    90.     private void UpdateFocus()
    91.     {
    92.         if( m_cam == null ) return;
    93.  
    94.         if( m_dst == null ) return;
    95.  
    96.  
    97.         float t   = Time.unscaledTime - m_transition_start;
    98.  
    99.         float prc = Mathf.Clamp01( m_curve.Evaluate( t ) );
    100.  
    101.         m_cam.transform.position = Vector3.Lerp   ( m_start_pos, m_dst.position, prc );
    102.  
    103.         m_cam.transform.rotation = Quaternion.Lerp( m_start_rot, m_dst.rotation, prc );
    104.  
    105.  
    106.         if( prc >= 1.0f )
    107.         {
    108.             Transform  poi = ( m_dst != m_POI )  ?  m_POI : null;
    109.  
    110.             float   freeze_duration = ( poi == m_POI ) && ( m_POI != null ) ? m_showcase_duration : 0.0f;
    111.  
    112.             Freeze( freeze_duration, () => { m_dst = null; Focus( poi ); } );
    113.         }
    114.     }
    115.  
    116.     //********************************************************************************
    117.     //
    118.     //********************************************************************************
    119.  
    120.     private void UpdateFreeze()
    121.     {
    122.         if( m_freeze_start > 0.0f )
    123.         {
    124.             if( ( Time.unscaledTime - m_freeze_start ) >= m_freeze_duration )
    125.             {
    126.                 m_freeze_start = 0.0f;
    127.  
    128.                 if( m_freeze_cb != null ) { m_freeze_cb(); m_freeze_cb = null; }
    129.             }
    130.         }
    131.     }
    132.  
    133.     //********************************************************************************
    134.     //
    135.     //********************************************************************************
    136.  
    137.     private void Awake()
    138.     {
    139.         if( ( m_POI != null ) && ( m_cam != null ) )
    140.         {
    141.             m_cam.transform.position = m_POI.position;
    142.  
    143.             m_cam.transform.rotation = m_POI.rotation;
    144.         }
    145.     }
    146.  
    147.     //********************************************************************************
    148.     //
    149.     //********************************************************************************
    150.  
    151.     private void Start() { ProductSlot.Sort(); }
    152.  
    153.     //********************************************************************************
    154.     //
    155.     //********************************************************************************
    156.  
    157.     private void Update()
    158.     {
    159.         UpdateFreeze();
    160.  
    161.         UpdateFocus ();
    162.     }
    163. }
    164.  
    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.UI;
    7.  
    8. //************************************************************************************
    9. //
    10. //************************************************************************************
    11.  
    12. public class ShowcaseUI : MonoBehaviour
    13. {
    14.     //********************************************************************************
    15.     //
    16.     //********************************************************************************
    17.  
    18.     public struct BUTTON
    19.     {
    20.         public readonly GameObject self;
    21.  
    22.         public readonly Image      img;
    23.                                    
    24.         public readonly Button     but;
    25.                                    
    26.         public readonly Text       txt;
    27.  
    28.         public BUTTON( GameObject obj )
    29.         {
    30.             Transform xform = ( obj != null ) ? obj.transform : null;
    31.  
    32.             self = obj;
    33.  
    34.             img  = ( xform != null ) ? xform.GetComponent< Image  >() : null;
    35.                  
    36.             but  = ( xform != null ) ? xform.GetComponent< Button >() : null;
    37.                  
    38.             txt  = ( xform != null ) && ( xform.childCount > 0 ) ? xform.GetChild( 0 ).GetComponent< Text >() : null;
    39.         }
    40.     }
    41.  
    42.     //********************************************************************************
    43.     //
    44.     //********************************************************************************
    45.  
    46.     static private Font DEFAULT_FONT = null;
    47.  
    48.     static private void LoadDefaultFont() { if( DEFAULT_FONT == null ) DEFAULT_FONT = Resources.GetBuiltinResource< Font >( "Arial.ttf" ); }
    49.  
    50.     //********************************************************************************
    51.     //
    52.     //********************************************************************************
    53.  
    54.     [ SerializeField ] private Showcase                m_showcase  = null;
    55.  
    56.     [ SerializeField ] private Transform               m_bts_grp   = null;
    57.                                                                    
    58.     [ NonSerialized  ] private readonly List< BUTTON > m_bts       = new List< BUTTON >();
    59.  
    60.     [ NonSerialized  ] private int                     m_selection = -1;
    61.  
    62.     //********************************************************************************
    63.     //
    64.     //********************************************************************************
    65.  
    66.     private GameObject CreateDefaultButton( Transform parent )
    67.     {
    68.         GameObject bt  = new GameObject( "bt",  typeof( Image ), typeof( Button ) );
    69.  
    70.         GameObject txt = new GameObject( "txt", typeof( Text   ) );
    71.  
    72.         txt.transform.SetParent( bt.transform, false );
    73.  
    74.         bt.transform.SetParent ( parent,       false );
    75.  
    76.  
    77.         RectTransform xform = txt.transform as RectTransform;
    78.  
    79.         if( xform != null )
    80.         {
    81.             xform.anchorMin     = new Vector2( 0.0f, 0.0f );
    82.                                    
    83.             xform.anchorMax     = new Vector2( 1.0f, 1.0f );
    84.  
    85.             xform.pivot         = new Vector2( 0.5f, 1.0f );
    86.  
    87.             xform.localPosition = Vector3.zero;
    88.  
    89.             xform.offsetMin     = Vector2.zero;
    90.                                    
    91.             xform.offsetMax     = Vector2.zero;
    92.         }
    93.  
    94.         return bt;
    95.     }
    96.  
    97.     //********************************************************************************
    98.     //
    99.     //********************************************************************************
    100.  
    101.     private void CreateDependencies()
    102.     {
    103.         if( m_bts_grp == null ) return;
    104.  
    105.         GameObject existing = ( m_bts_grp.childCount > 0 ) ? m_bts_grp.GetChild( 0 ).gameObject : null;
    106.  
    107.         GameObject template = ( existing  != null ) ? existing : CreateDefaultButton( m_bts_grp );
    108.  
    109.  
    110.         int nb_available = m_bts_grp.childCount;
    111.  
    112.         int nb_requested = ProductSlot.instances.Count;
    113.  
    114.         int nb_to_create = ( nb_requested > nb_available ) ? nb_requested - nb_available : 0;
    115.  
    116.  
    117.         if( template != null )
    118.         {
    119.             for( int b = 0; b < nb_available; ++b ) { m_bts.Add( new BUTTON( m_bts_grp.GetChild( b ).gameObject ) ); }
    120.  
    121.             for( int b = 0; b < nb_to_create; ++b ) { GameObject obj = Instantiate( template, m_bts_grp ); m_bts.Add( new BUTTON( obj ) ); }
    122.         }
    123.     }
    124.  
    125.     //********************************************************************************
    126.     //
    127.     //********************************************************************************
    128.  
    129.     private void SetupDependencies()
    130.     {
    131.         for( int b = 0, count = m_bts.Count; b < count; ++b )
    132.         {
    133.             BUTTON button = m_bts[ b ];
    134.  
    135.             int bt_capture = b;
    136.  
    137.             if( button.but  != null ) { button.but.onClick.AddListener( () => OnButton( bt_capture ) ); }
    138.                            
    139.             if( button.txt  != null ) { button.txt.text = "PRODUCT " + b.ToString( "D3" ); button.txt.font = DEFAULT_FONT; button.txt.color = Color.black; button.txt.alignment = TextAnchor.MiddleCenter; }
    140.  
    141.             if( button.self != null ) { button.self.SetActive( b < ProductSlot.instances.Count ); }
    142.         }
    143.     }
    144.  
    145.     //********************************************************************************
    146.     //
    147.     //********************************************************************************
    148.  
    149.     private void OnButton( int bt )
    150.     {
    151.         m_selection = bt;
    152.  
    153.         enabled  =  true;
    154.     }
    155.  
    156.     //********************************************************************************
    157.     //
    158.     //********************************************************************************
    159.  
    160.     private void Awake() { LoadDefaultFont(); }
    161.  
    162.     private void Start() { CreateDependencies(); SetupDependencies (); enabled = false; }
    163.  
    164.     //********************************************************************************
    165.     //
    166.     //********************************************************************************
    167.  
    168.     private void Update()
    169.     {
    170.         if( m_selection >= 0 )
    171.         {
    172.             if( ( m_showcase != null ) && ( m_selection < ProductSlot.instances.Count ) ) m_showcase.Focus( ProductSlot.instances[ m_selection ].POI );
    173.  
    174.             m_selection = -1;
    175.         }
    176.  
    177.         enabled = false;
    178.     }
    179. }
    180.  
     
    Last edited: Dec 19, 2021
  4. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,335
    @Serge_Billault it's very kind of you to provide a ready made solution for the OP.

    However: I don't think dumping this much code without any comments on a self-proclaimed newbie is the way to go. Also the code looks generated from some sort of UI builder. It's not good quality imho. Sorry for the critique, I don't mean to discourage you.

    @dineshlancerevo I would recommend to follow Kurt-Dekkers suggestion. Take a look into Cinemachine. It seems like a very good fit for your problem. Give it a try and once you get stuck (and you most likely will) come back to the forum and ask for help again.

    Oh and btw.: welcome to the Unity forum :)
     
  5. Serge_Billault

    Serge_Billault

    Joined:
    Aug 26, 2014
    Posts:
    190
    @_geo__: Exemple made from scratch in 30 minutes. Why would you wish I was discouraged any way.
     
  6. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
  7. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,335
    @Serge_Billault Well, I was criticizing the example you posted and I recognize the effort (30 mins of your time).
    The first response to your post was some random guy (me) kinda disliking it. I know from experience that it stings. Just wanted to let you know I appreciate what you did, despite my critique.
     
    ZO5KmUG6R likes this.
  8. Serge_Billault

    Serge_Billault

    Joined:
    Aug 26, 2014
    Posts:
    190
    If that can reassure you, I dont give a F***. Mainly because the guy asked if we could send him some script, which is no effort.
     
  9. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,335
    "Give a man a fish, and you feed him for a day. Teach a man to fish, and you feed him for a lifetime."
     
    ZO5KmUG6R likes this.
  10. Serge_Billault

    Serge_Billault

    Joined:
    Aug 26, 2014
    Posts:
    190
    I'm ok to continue this discussion at the condition that you make it clear to the moderators that it is you who insist on making me the topic of this thread.
     
  11. dineshlancerevo

    dineshlancerevo

    Joined:
    Dec 6, 2021
    Posts:
    2
    Thank You Kurt, Serge and Geo. I will start learning C#.

    Serge@ Thanks for taking ur time bro
    Geo@ I respect your perception as well bro.

    Mods@ You may close or remove this thread (if already exists).
     
    _geo__ likes this.