- Home /
The question is answered, right answer was accepted
How to make the camera follow after the selected character?
i have a selection character script `using System.Collections; using System.Collections.Generic; using UnityEngine;
using UnityEngine.SceneManagement; public class skinselect : MonoBehaviour { private GameObject[] skinlist; public int index;
// Start is called before the first frame update
private void Start()
{
index = PlayerPrefs.GetInt("CharacterSelected");
skinlist = new GameObject[transform.childCount];
for(int i =0; i < transform.childCount; i++)
skinlist[i] = transform.GetChild(i).gameObject;
foreach(GameObject go in skinlist)
{
go.SetActive(false);
if (skinlist[index])
skinlist[index].SetActive(true);
}
}
public void ToggleLeft()
{
skinlist[index].SetActive(false);
index--;
if (index < 0)
index = skinlist.Length - 1;
skinlist[index].SetActive(true);
}
public void Toggleright()
{
skinlist[index].SetActive(false);
index++;
if (index ==skinlist.Length)
index = 0;
skinlist[index].SetActive(true);
}
public void confirmbutton()
{
PlayerPrefs.SetInt("CharacterSelected", index);
}
}
and camera follow script
using UnityEngine;
public class camerafollow : MonoBehaviour {
public Transform player;
public float smoothSpeed = 0.125f;
public Vector3 offset;
void LateUpdate()
{
transform.position = player .position + offset;
}
}
but i dont know how to make the camera follow after the selected character
Answer by ahsen35813 · Mar 29, 2020 at 04:21 PM
I would recommend using this script. It works perfectly for me so it might be what you're looking for. Just make sure to add the intended player to be targeted into the public variable. This script will find the current location of the camera in relation to the targeted object and then maintain that location in relation to the target as the player moves around. If your player does not rotate, then you can just add the camera as a child of the player instead of needing this script.
public class CameraController : MonoBehaviour
{
public GameObject Target;
private Vector3 offset;
// Start is called before the first frame update
void Start()
{
offset = transform.position - Target.transform.position;
}
// Update is called once per frame
void LateUpdate()
{
transform.position = Target.transform.position + offset;
}
}
If you want this script to automatically find the player, you can add this script into the Start()
block.
Target = gameObject.Find("PlayerName");
Replacing PlayerName with the name of the target for the camera and making the Target variable private.
I hope this helps!
tnx a lot man, the first method is working, but it follows only after the character list, and in the list i have 3 skins of ball that is moving forward and the camera is staying with the list at the start.
and in the second method i can only put one skin name so thats not working
$$anonymous$$aybe an extra bit to the script that takes the variable of the skin selected to automatically set the target for the camera?
Something like this might work:
private int skinSelection;
public void SkinSelectionComplete()
{
skinSelection = gameObject.Find("BallList").GetComponent<SkinSelector>().skinSelected;
if (skinSelection == 0)
{
Target = gameObject.Find("Red Ball");
}
else if
{
Target = gameObject.Find("Green Ball");
}
else if
{
Target = gameObject.Find("Blue Ball");
}
}
You will need a UI button that you click after selecting your player, and you can put SkinSelectionComplete()
into the OnClick() area in the button. You will have to replace BallList with the name of the character list that you mentioned, replace SkinSelector with the name of the script on the ball list, and replace skinSelected with the public variable name containing the data about which skin was selected in the form of an int. Hope that helps!
Please note that I haven't tested this so you might have to do some debugging.
Why use that script when you can just use out of the box unity functionality? Its not doing anyting that parenting game objects doesnt offer with no code.
Yes, but that only works if the player's orientation is not changing. For stixy_boi's situation, he has a rolling ball I think, so making the camera a child of the ball would make the camera roll around with the ball. You could give the ball zero friction to make that work, but that's not the right way to do it and this script gives more customizability as well.
Follow this Question
Related Questions
change objects Axis to match cameras rotation 0 Answers
Display object panel in front of camera with correct rotation and position (no parent) 0 Answers
Check if position is on screen 0 Answers
URGENT Cinemachine virtual camera while falling 1 Answer
Camera follow player while it doesnt block animations 1 Answer