I am having issues creating a third-person camera for my Rogue Squadron (N64) like game. First I have posted my issue on the official Unity Answers site here. So far my ship is behaving as I want and the camera follows position correctly (XYZ), however, it doesn't rotate how I would like. My first attempt was to set the rotation of the camera to the rotation of the ship and realized that is not what I am looking for either. I have found some information on rotating around a (pivot) point but I am having trouble understanding them. For example, I have found this and this but Im not sure how they work. The end result would be something like the camera found here. Thank you for any help, I would greatly appreciate a description of what you are doing/how it works. Lastly this is done in C# in Unity. I can provide my code if needed.
1 Answer
Hello and welcome to the community!
Solution 1 - manually setting rotation and position
You almost got it, you just need to do camera offset a bit different. Transform
has handy-dandy properties, called .foward
, .right
, which are normalized vectors for the direction the Transform
is looking at.
Now imagine your player ir facing north (transform.forward
is (x=0, y=0, z=1)
). To move camera 5 units behind, you would do cameraPosition = playerPosition + playerTransform.forward * -5f
. The multiplication would result in (x=0, y=0, z=-5)
and camera would be placed 5 units behind. Easy, right?
Now what if you want to place 5 units behids plus two units to the left? You add those Vectors together, which results in cameraPosition = playerPosition + ((playerTransform.forward * -5f) + (playerTransform.right * -4f))
. Now your camera is placed 5 units behind and 4 unit left of the player.
To place the camera 2 units above the player, you would just use transform.up
property and the whole routine is the same.
After setting the offset, use camera.rotation = player.rotation
to rotate camera to right angle.
Here's a quick snippet for camera, that follows set object:
public class TransformFollower : MonoBehaviour
{
public Vector3 offset;
public Transform target;
private void Update()
{
transform.position = target.position +
(target.right * offset.x) +
(target.up * offset.y) +
(target.forward * offset.z);
transform.rotation = target.rotation;
}
}
Solution 2 - let Unity hierarchy do it for you
If you're never going to Destroy player character object, you could make the camera as a child of the Player object. Then setting offset in the inspector. All rotation and following would be done automatically by the Unity engine, as the Camera would be anchored to the Player.
-
\$\begingroup\$ Hello and thank you Tomas for your input / help. So I have read your post and I like it a lot though there are still some issues The issue is the camera will not rotate with the ship's local z. The result is I can fly upside down and the camera will still be right side up. I have a video here. Thank you again for your help. \$\endgroup\$– ShamusCommented Apr 10, 2020 at 21:07
-
\$\begingroup\$ Ok, found the solution - instead of using
transform.LookAt()
just makecamera.rotation = player.rotation
. I'm editing the answer to make it clear. \$\endgroup\$– eLTomisCommented Apr 11, 2020 at 12:19 -
1\$\begingroup\$ Hey, Tomas thank you so much your answer is working. After learning from my mistakes it seems I nearly had it though I was setting the position of the camera to the position of the ship not the "forward" of the ship. Again, I appreciate all of your help, thank you. \$\endgroup\$– ShamusCommented Apr 11, 2020 at 16:58