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

Resolved How To Change Camera Target Based on Index/Character Selected?

Discussion in 'Scripting' started by AdamEarlston, May 23, 2021.

  1. AdamEarlston

    AdamEarlston

    Joined:
    Jun 7, 2019
    Posts:
    71
    Hey, so basically my issue is that i use a Character Select list (GameObject) with characters as a child of that game object, but when i set the Main Camera to target the Character Select List, it doesn't follow the character (player).
    So basically i need to make the camera's Target change to which character i select somehow, i tried adding this "
    Code (CSharp):
    1. cameraFollow.Target = characterList[index].transform;
    " to Left and Right toggle, but the camera still seems to follow the Character Select List instead of the selected Character.

    So when I select/choose Player 3 (which is Index 3 or something like that) i need the Camera's Target to switch to Player 3's Transform, instead of the CharacterList's Transform.

    Script attached to the Main Camera
    upload_2021-5-23_8-31-14.png

    Parents/Children
    upload_2021-5-23_8-36-25.png

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class CharacterSelection : MonoBehaviour
    7. {
    8.     private GameObject[] characterList;
    9.     private int index;
    10.     public DampCamera2D cameraFollow;
    11.  
    12.  
    13.     private void Start()
    14.     {
    15.         index = PlayerPrefs.GetInt("CharacterSelected");
    16.  
    17.         characterList = new GameObject[transform.childCount];
    18.  
    19.         //Fill the array with our models
    20.         for(int i = 0; i < transform.childCount; i++)
    21.             characterList[i] = transform.GetChild(i).gameObject;
    22.  
    23.         //We toggle off their renderer (invisible)
    24.         foreach (GameObject go in characterList)
    25.             go.SetActive(false);
    26.  
    27.         //We toggle on the selected character
    28.         if (characterList[index])
    29.             characterList[index].SetActive(true);
    30.     }
    31.  
    32.     public void ToggleLeft()
    33.     {
    34.         //Toggle off the current model
    35.         characterList[index].SetActive(false);
    36.  
    37.         index--;
    38.         if (index < 0)
    39.             index = characterList.Length - 1;
    40.  
    41.         //Toggle on the new model
    42.         characterList[index].SetActive(true);
    43.         cameraFollow.Target = characterList[index].transform;
    44.     }
    45.  
    46.     public void ToggleRight()
    47.     {
    48.         //Toggle off the current model
    49.         characterList[index].SetActive(false);
    50.  
    51.         index++;
    52.         if (index == characterList.Length)
    53.             index = 0;
    54.  
    55.         //Toggle on the new model
    56.         characterList[index].SetActive(true);
    57.         cameraFollow.Target = characterList[index].transform;
    58.     }
    59.  
    60.     public void ConfirmButton()
    61.     {
    62.         PlayerPrefs.SetInt("CharacterSelected", index);
    63.         SceneManager.LoadScene("Menu");
    64.     }
    65. }
    66.  
    (Ignore the 2D part in name, it's a 3D game)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DampCamera2D : MonoBehaviour
    5. {
    6.     // camera will follow this object
    7.     public Transform Target;
    8.     //camera transform
    9.     public Transform camTransform;
    10.     // offset between camera and target
    11.     public Vector3 Offset;
    12.     // change this value to get desired smoothness
    13.     public float SmoothTime = 0.3f;
    14.  
    15.     // This value will change at the runtime depending on target movement. Initialize with zero vector.
    16.     private Vector3 velocity = Vector3.zero;
    17.  
    18.     private void Start()
    19.     {
    20.         Offset = camTransform.position - Target.position;
    21.     }
    22.  
    23.     private void FixedUpdate()
    24.     {
    25.         // update position
    26.         Vector3 targetPosition = Target.position + Offset;
    27.         camTransform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, SmoothTime);
    28.  
    29.         // update rotation
    30.      
    31.     }
    32. }
     
    Last edited: May 23, 2021
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777
    Hi,

    One suggestion is to check if the Target is changing when running the game in the Editor.
    And click on it, to make the Editor show which game object its targeting. Is it the right one, or another?

    Also put some Debug.Log in the code to see which parts of the code are running, or not.
     
    AdamEarlston likes this.
  3. AdamEarlston

    AdamEarlston

    Joined:
    Jun 7, 2019
    Posts:
    71
    Welp i thought i already checked to see if the Camera Target was changing correctly lol, but i'm an idiot and i don't think i did check the correct thing, anyway it works correctly on the first scene, but after i change scene, the Main Camera goes back to having the CharacterList as the Target, instead of Player 3 that was just selected in the character select scene. Which means i'm not saving the information in the PlayerPrefs correctly? or the Main Camera in the new scene isn't having its info saved or transfered maybe.
     
  4. AdamEarlston

    AdamEarlston

    Joined:
    Jun 7, 2019
    Posts:
    71
    So i'm pretty sure the issue is that i'm not using PlayerPrefs to save the Target of the camera to the camera in the next scene, which i have no idea how to do sadly.
     
  5. AdamEarlston

    AdamEarlston

    Joined:
    Jun 7, 2019
    Posts:
    71
    I have figured it out now :p Thanks! My brain was a little slow on realizing the issue, which was that i didn't apply this code
    Code (CSharp):
    1. cameraFollow.Target = characterList[index].transform;
    in the private void Start() section after it sets the chosen player to be active.
     
    arfish likes this.