Hi guys, I’ve been working on this for 3 days now and I can’t for the life of me figure it out!
My game is a 3D turn based football game where you drag the players back to add power and let go to ‘catapult’ them towards the direction you are aiming in. The game can be found here Game Jolt - Share your creations
Basically when I right click a player, I want the camera to go behind the player, facing the ball object like below (the player circled is the one that has been clicked):
I want the camera to always be a set distance away from the player no matter what the position of the ball is, but always facing towards the ball.
The best way I can describe it is that I want a player quick select where you can jump from player to player easily to line a shot up.
I would be very grateful if someone could point me in the right direction as this is driving me insane! I hope I’ve managed to describe my problem in a clear way.
You take the direction from the player to the ball along the ground, multiply it by the distance you want the camera to be away from the player, subtract that from the player position and move it up a little. Finally, you just make the camera look at the ball.
Vector3 dir = (ball.position - player.position);
dir = new Vector3(dir.x, 0, dir.z).normalized;
camera.position = player.position - dir * distance + Vector3.up * height;
You need to create a script to do this, I will not write the code correctly because i’m not used to use this type of functions, you must look for the correct name of functions and how write them, but the idea must be this:
You have 3 elements, Ball, Player, Camera.
First, using always the ball as reference, you need to calculate the direction from the ball to the selected player (ball.position-player.position).
Then calculate the distance (using Vector3.Distance) between ball and the player.
Once you know the direction and the distance, you should be able to move the camera to the same direction and (for example) 2 times the distance player-ball.
So now will have always a “imaginary line ball-player-camera”.
And dont forget to make the camera look at the ball with “LookAt” function.
The concept of your script sholud be thisone. Now, It’s your turn to find the functions to do it!
If helped, accept the answer and close the question!