- Home /
Rotate camera around ball; always face "back" of ball?
I'm trying to get my camera to rotate around my ball when I turn left and right. I'm using the Update method and:
void Update () {
//transform.eulerAngles = new Vector3 (1, 1, 0);
transform.RotateAround(player.transform.position, player.transform.eulerAngles, 0);
}
but the camera just goes all spastic. How can I keep my camera facing the back of my ball as if it were a person and we were always behind the person who is walking?
Why don't you simply put your camera as a child of your ball ? Thus, when your ball will turn, the camera will follow the ball and stay behind it.
By the way, in the standard assets, there is a Character Controller which is based on this principle (not sure)
Answer by Baste · Jun 23, 2015 at 10:36 PM
RotateAround rotates your transform around a point, with regards to an axis, a number of degrees. Read the docs here, there's a good example.
If you want to keep the camera at a fixed position and rotation with regards to the player character, @Hellium's comment is the best bet - make the camera a child of the player, and skip the scripting. If that's not viable, this is a super-cheap implementation:
float cameraVDistance = 10f; //how far the camera is behind the player
float cameraHDistance = 5f; //how far the camera is above the player
void Update() {
Vector3 playerPosition = player.transform.position;
Vector3 behindPlayer = -player.transform.forward * cameraVDistance;
Vector3 abovePlayer = Vector3.up * cameraHDistance;
Vector3 cameraPosition = playerPosition + behindPlayer + abovePlayer;
Vector3 cameraToPlayer = playerPosition - cameraPosition;
Quaternion cameraRotation = Quaternion.LookRotation(cameraToPlayer);
transform.position = cameraPosition;
transform.rotation = cameraRotation;
}
That's put the camera behind the player, looking at the player. You'll want to mess with the values somewhat, and the camera should probably be looking at a point a bit above the player (so you don't just see mostly ground), but that's a quick start.
Honestly, childing the camera to the player character is easier, but there you go.
Thank you, i will try this out now. I have childed my cam with my player but it doesn't work that way. I don't know how to explain it, but I'll see how this goes. Thanks again
@Baste I've trying to figure out why this code is rotating the camera forwards over and underneath the ball even when I'm not turning left or right. That's the issue I'm having in the first place. I set my cam as child, but still getting the same result.
Your answer
Follow this Question
Related Questions
Visibility of objects 2 Answers
How do I make a Game Object invisible? 1 Answer
Camera Effect + Translating US to C# 3 Answers
Multiple Cars not working 1 Answer
I want to put main camera floating 0 Answers