- Home /
Camera change target onclick
Hi, I'm creating a very simple 3d platformer with multiple characters in the scene. my camera follows the "main character" when he walks around, the other characters do the idle-animation while inactive.
now the problem: i want to switch between the active character with a mouseclick. when an another character is clicked, my movement_script AND the following main-camera (not a child or parent - get its focuspoint via 'target') should be transferred or activated to this character, the old one shall idle.
Im really new to Unity and this Community and I have tried to look for an answer but couldn't find anything.
Every help is much appreciated, sorry for my bad English - its not my native language. If you need any more infos, just ask.
I dont expect a full code template, but at least a little help to resolve this problem.
thank you in advantage!
Answer by iJuan · May 13, 2018 at 04:40 PM
You need to throw a Raycast in the position the mouse clicked and check if it collides with any player
void Update() {
if(Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mouse Position);
foreach (RaycastHit hit in Physics.RaycastAll(ray)) {
if (hit.transform.gameObject.tag == "Player") {
//hit.transform is the transform of the clicked player
}
}
}
}
Your answer
