- Home /
Third Person Camera on a speherical world
Hi all, I am trying to implement a third person camera which will rotate around the player’s X and Y axis. My player is always facing upright towards the planet’s center meaning the Y direction changes depending where you are on the planet. The problem is rotating around the player’s local space.
Desired outcome is a camera controller like in Grow Up.
If anyone can help me with a solution it would be highly appreciated, thanks.
Answer by Buckslice · Dec 12, 2017 at 10:36 PM
I would setup my Player hierarchy like so
Player
CamRot
Camera
The Player rotation gets rotated based on the planets gravity, seems like you already have this working.
Mouse X would either rotate the player around the players up vector (if you want player to look in direction of mouse movement) or it would just set CamRots y rotation (you could rotate the player transform elsewhere based on player movement directions).
Mouse Y would set CamRots x rotation, and mouse zooming would change the z position of Camera. Let me know if you need more clarification, I could try writing up a simple script for this later. If it is confusing try creating the simple hierarchy and manually change the rotations to get a better understanding. Good luck!
Hi Buckslice, thank you for the information, I will play around with the hierarchy and rotations tonight. I will let you know if it works.
Just for interest sake, would there be a different method that doesn’t involve parenting the camera to the player?
Answer by EliteFrame · Dec 13, 2017 at 04:31 PM
For anyone else interested, I have figured out a method how to make an orbital camera controller without parenting the camera to the player.
First things first(thanks to @Buckslice) I was playing around with the hierarchy and did this: Create 2 different empty game objects "CameraRotY" and "CameraRotX" and place the in the hierarchy as follows:
CameraRotY
CameraRotX
Camera
You can offset the "Camera" wherever you like behind the player and scale the "CameraRotY" for the distance between the player and camera. Create a empty game object that is centred and parented on the player or roughly where your pivot point is going to be. This Empty will basically be the target for "CameraRotY" to be in. You can then either add a smooth follow or set the position of "CameraRotY" to be at the target on the player. Then add this Java script to CameraRotY and Drag the game object which is the planet into the sphere variable.
#pragma strict
var sphere : Transform;
function Update() {
var v3 = transform.position - sphere.position;
transform.rotation = Quaternion.FromToRotation(transform.up, v3) * transform.rotation;
}
This will lock the x and z rotations on CameraRotY so only the up vector will be influenced by rotation.
Finally you can rotate CameraRotY's Y axis and CameraRotX's X axis with mouse x and mouse y input. Also just remember to give X's rotation a float clamp if that is what you need.
Your answer
Follow this Question
Related Questions
Weird behavior when setting eulerAngles directly 2 Answers
Why is rotation locking to an axis? 3 Answers
need my object to rotate between specific angles on the x axis 2 Answers
Make Quaternion affected by float 1 Answer
Reverse rotations problems... 2 Answers