Search Unity

how to make camera follow different characters?

Discussion in 'Scripting' started by janjicm, Apr 29, 2020.

  1. janjicm

    janjicm

    Joined:
    Apr 29, 2020
    Posts:
    16
    I watched this tutorial:


    everything works great except that I can't make camera follow different character. I have 2 balls, and putting camera as a child of player doesn't work.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Post the code for the camera follower script? (not gonna sit through a half hour video to find it)
     
  3. janjicm

    janjicm

    Joined:
    Apr 29, 2020
    Posts:
    16
    I'm sorry, wasn't on computer.

    CameraFollow script that worked before adding more characters:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class CameraFollow : MonoBehaviour
    {
    public Transform target;

    public float smoothSpeed = 10f;
    public Vector3 offset;

    void FixedUpdate ()
    {
    Vector3 desiredPosition = target.position + offset;
    Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed * Time.deltaTime);
    transform.position = desiredPosition;

    transform.LookAt(target);
    }

    }


    Here is the script for CharacterSelection:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;


    public class CharacterSelection : MonoBehaviour
    {
    private GameObject[] characterList;
    private int index;


    private void Start()
    {
    index = PlayerPrefs.GetInt("CharacterSelected");

    characterList = new GameObject[transform.childCount];

    for (int i = 0; i < transform.childCount; i++)
    characterList = transform.GetChild(i).gameObject;

    foreach (GameObject go in characterList)
    go.SetActive(false);

    if (characterList[index])
    characterList[index].SetActive(true);

    }


    public void ToggleLeft()
    {
    characterList[index].SetActive(false);

    index--;
    if (index < 0)
    index = characterList.Length - 1;

    characterList[index].SetActive(true);
    }


    public void ToggleRight()
    {
    characterList[index].SetActive(false);

    index++;
    if (index == characterList.Length)
    index = 0;

    characterList[index].SetActive(true);
    }

    public void ConfirmButton()
    {
    PlayerPrefs.SetInt("CharacterSelected", index);
    SceneManager.LoadScene("menu");
    }


    }
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    Looks like that script has a "target" field for the thing that the Camera should follow. Have you tried setting your player as the target?
     
  5. janjicm

    janjicm

    Joined:
    Apr 29, 2020
    Posts:
    16
    Yes I did. As I said, it worked fine while I had only one character. After that, if I set ball1 for my character in target it will follow it, if I choose ball2 it wont follow. Setting whole characterised in target doesn't work
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    I don't see how this "Character Selection" script is relevant to the camera following. The camera follow script will follow its "target". If nothing changes the target field on the camera (which it doesn't seem like the character selection script is doing), the camera script is not going to do anything other than continuing to follow its old target.
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Is "ball2" a separate GameObject, or a child of some other object? How did you set the target reference to ball2, and what debugging did you do to confirm it really was set correctly? What happens when you try just manually setting the reference to ball2 via the inspector during play mode?
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    When posting code, use code tags, otherwise it'll get mangled as it has here (e.g. you have an [ i ] in your code and then it became italics, in addition to just mangling all the whitespace etc).

    So ToggleLeft and ToggleRight switch to the previous/next characters, right? So in those functions is where you need to set the CameraFollow's .target. That does mean you'll need a reference to your CameraFollow in your CharacterSelection. So in CharacterSelection:
    Code (csharp):
    1. public CameraFollow cameraFollow; //drag to assign this in the inspector
    2.  
    3. public void ToggleLeft() {
    4. ...
    5. cameraFollow.target = characterList[index].transform;
    6. }
    With the same in ToggleRight.
     
    Joe-Censored likes this.
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    It's relevant because CharacterSelection is the script that knows which object needs to be assigned to CameraFollow's target.
     
    Joe-Censored likes this.