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. Dismiss Notice

Move to Next Camera position

Discussion in 'Scripting' started by Vetlecn, Aug 4, 2021.

  1. Vetlecn

    Vetlecn

    Joined:
    Jul 11, 2021
    Posts:
    6
    I am working on a project where I have multiple cameraPositions (HotSpots) in a public GameObject []. I want to move the Main Camera to next/previous HotSpot when pressing Left/Right arrows. The cameraPositions are placed in scene as Empty Game Objects with desired rotation and position.
    I guess my main question is:
    How do I get the World Position and rotation of the next/ previous item in a list/array?

    Thanks!
     
  2. chatrat12

    chatrat12

    Joined:
    Jan 21, 2015
    Posts:
    122
    Something like this?:


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CameraCycle : MonoBehaviour
    4. {
    5.     private const KeyCode PREV_CAMERA_KEY = KeyCode.LeftArrow;
    6.     private const KeyCode NEXT_CAMERA_KEY = KeyCode.RightArrow;
    7.  
    8.     [SerializeField] private Transform[] m_cameraPositions;
    9.     [SerializeField] private Camera m_Camera;
    10.     private int _currentCameraIndex = 0;
    11.    
    12.  
    13.     private void Start()
    14.     {
    15.         SetCameraNewIndex(0);
    16.     }
    17.     private void Update()
    18.     {
    19.         if (Input.GetKeyDown(PREV_CAMERA_KEY))
    20.             SetCameraNewIndex(_currentCameraIndex - 1);
    21.         if (Input.GetKeyDown(NEXT_CAMERA_KEY))
    22.             SetCameraNewIndex(_currentCameraIndex + 1);
    23.     }
    24.  
    25.     private void SetCameraNewIndex(int newIndex)
    26.     {
    27.         _currentCameraIndex = ClampIndex(newIndex);
    28.         m_Camera.transform.position = m_cameraPositions[_currentCameraIndex].position;
    29.         m_Camera.transform.rotation = m_cameraPositions[_currentCameraIndex].rotation;
    30.     }
    31.  
    32.     private int ClampIndex(int index)
    33.     {
    34.         if (index >= m_cameraPositions.Length) return 0;
    35.         else if (index < 0) return m_cameraPositions.Length - 1;
    36.         return index;
    37.     }
    38. }
     
    Vetlecn likes this.
  3. Vetlecn

    Vetlecn

    Joined:
    Jul 11, 2021
    Posts:
    6
    Is it possible to lerp or MoveTowards?
     
  4. Vetlecn

    Vetlecn

    Joined:
    Jul 11, 2021
    Posts:
    6
    How can I make it only move to nxt/prev cam position if m_Camera.transform.position = one of the hotspots position?
    Here is how I solved the Lerp/MoveTowards "problem":
    Appreciate feedback!


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class HotSpotCycling : MonoBehaviour
    4. {
    5.     private const KeyCode PREV_CAMERA_KEY = KeyCode.LeftArrow;
    6.     private const KeyCode NEXT_CAMERA_KEY = KeyCode.RightArrow;
    7.     private const KeyCode stopRot = KeyCode.Mouse0;
    8.  
    9.     [SerializeField] private Transform[] m_cameraPositions;
    10.     [SerializeField] private Camera m_Camera;
    11.     private int _currentCameraIndex;
    12.     private float speed = 0.4f;
    13.     private float camRotSpeed = 5f;
    14.     private bool nextPos = false;
    15.     private bool prevPos = false;
    16.     private bool Rotation = true;
    17.  
    18.     void Update()
    19.     {
    20.             if (Input.GetKeyDown(PREV_CAMERA_KEY))
    21.                 SetCameraNewIndex(_currentCameraIndex - 1);
    22.             if (Input.GetKeyDown(PREV_CAMERA_KEY))
    23.             {
    24.    
    25.                 prevPos = true;
    26.                 Rotation = true;
    27.                
    28.             }
    29.  
    30.             if (Input.GetKeyDown(NEXT_CAMERA_KEY))
    31.                 SetCameraNewIndex(_currentCameraIndex + 1);
    32.             if (Input.GetKeyDown(NEXT_CAMERA_KEY))
    33.             {
    34.                
    35.                 nextPos = true;
    36.                 Rotation = true;
    37.                
    38.             }
    39.  
    40.             if (nextPos == true)
    41.             {
    42.                 SetCameraNewIndex(_currentCameraIndex);
    43.             }
    44.  
    45.             if (prevPos == true)
    46.             {
    47.                 SetCameraNewIndex(_currentCameraIndex);
    48.             }
    49.     }
    50.  
    51.     void SetCameraNewIndex(int newIndex)
    52.     {
    53.         _currentCameraIndex = ClampIndex(newIndex);
    54.         m_Camera.transform.position = Vector3.MoveTowards(
    55.             m_Camera.transform.position,
    56.             m_cameraPositions[_currentCameraIndex].position,
    57.             (Vector3.Distance(
    58.             m_Camera.transform.position,
    59.             m_cameraPositions[_currentCameraIndex].position)) / speed * Time.deltaTime);
    60.  
    61.         if (Rotation == true)
    62.         {
    63.             m_Camera.transform.rotation = Quaternion.Lerp(
    64.             m_Camera.transform.rotation,
    65.             m_cameraPositions[_currentCameraIndex].rotation,
    66.             camRotSpeed / (Vector3.Distance(
    67.             m_Camera.transform.position,
    68.             m_cameraPositions[_currentCameraIndex].position)) * Time.deltaTime);
    69.         }
    70.  
    71.         if (Input.GetKeyDown(KeyCode.W) ||
    72.             Input.GetKeyDown(KeyCode.A) ||
    73.             Input.GetKeyDown(KeyCode.S) ||
    74.             Input.GetKeyDown(KeyCode.D))
    75.         {
    76.             nextPos = false;
    77.             prevPos = false;
    78.         }
    79.  
    80.         if ((Input.GetKeyDown(stopRot)) || (Input.GetKey(stopRot)))
    81.         {
    82.             Rotation = false;
    83.         }
    84.     }
    85.  
    86.     int ClampIndex(int index)
    87.     {
    88.         if (index >= m_cameraPositions.Length) return 0;
    89.         else if (index < 0) return m_cameraPositions.Length - 1;
    90.         return index;
    91.     }
    92. }
     
  5. chatrat12

    chatrat12

    Joined:
    Jan 21, 2015
    Posts:
    122
    How is this?:

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class CameraCycle : MonoBehaviour
    5. {
    6.     private const KeyCode PREV_CAMERA_KEY = KeyCode.LeftArrow;
    7.     private const KeyCode NEXT_CAMERA_KEY = KeyCode.RightArrow;
    8.  
    9.     [SerializeField] private Transform[] m_cameraPositions;
    10.     [SerializeField] private Camera m_Camera;
    11.     [SerializeField] private float m_TransitionDuration = 0.5f;
    12.     private int m_CurrentCameraIndex = 0;
    13.     private bool m_Moving = false;
    14.  
    15.  
    16.     private void Start()
    17.     {
    18.         SetCameraNewIndex(0, true);
    19.     }
    20.     private void Update()
    21.     {
    22.         if (m_Moving) return;
    23.         if (Input.GetKeyDown(PREV_CAMERA_KEY))
    24.             SetCameraNewIndex(m_CurrentCameraIndex - 1);
    25.         if (Input.GetKeyDown(NEXT_CAMERA_KEY))
    26.             SetCameraNewIndex(m_CurrentCameraIndex + 1);
    27.     }
    28.  
    29.     private void SetCameraNewIndex(int newIndex, bool snap = false)
    30.     {
    31.         var start = m_cameraPositions[m_CurrentCameraIndex];
    32.         m_CurrentCameraIndex = ClampIndex(newIndex);
    33.         var end = m_cameraPositions[m_CurrentCameraIndex];
    34.         if (snap)
    35.         {
    36.             m_Camera.transform.position = end.position;
    37.             m_Camera.transform.rotation = end.rotation;
    38.         }
    39.         else
    40.             StartCoroutine(Lerp(start, end, m_TransitionDuration));
    41.     }
    42.  
    43.     private IEnumerator Lerp(Transform start, Transform end, float time)
    44.     {
    45.         var timer = 0f;
    46.         m_Moving = true;
    47.         while (timer < time)
    48.         {
    49.             m_Camera.transform.position = Vector3.Slerp(start.position, end.position, timer / time);
    50.             m_Camera.transform.rotation = Quaternion.Slerp(start.rotation, end.rotation, timer / time);
    51.             timer += Time.deltaTime;
    52.             yield return null;
    53.         }
    54.  
    55.         m_Camera.transform.position = end.position;
    56.         m_Camera.transform.rotation = end.rotation;
    57.         m_Moving = false;
    58.     }
    59.  
    60.     private int ClampIndex(int index)
    61.     {
    62.         if (index >= m_cameraPositions.Length) return 0;
    63.         else if (index < 0) return m_cameraPositions.Length - 1;
    64.         return index;
    65.     }
    66. }