1
\$\begingroup\$

I currently have a very basic scene that includes a platform (ground), a capsule (player), and a camera rotator (main camera is mounted on it). The object hierarchy is the following.

Scene's object hierarchy

This hierarchy reflects the objective of having a main camera that rotates about the player when correct command is entered. enter image description here

enter image description here

A problem arises when the player moves. In this case, the capsule rotates about its vertical axis which causes the main camera to rotate because the camera rotator is mounted on the player. How could I isolate the camera rotator so that it remains mounted while ignoring the capsule's rotation. Thanks for considering my question.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Many Unity tutorials start with just attaching the camera as a child to the player. While this is a quick and easy way to get going (while also demonstrating pretty well how transform hierarchies work), it's usually not how most real games end up controlling their camera.

If you want your camera to follow the position of an object but not its rotation, then it can be a good reason to separate your camera rig from its subject and add a script to the rig which makes it follow the subject. The most simple script would be this:

 class CameraController: MonoBehaviour {

        public Transform subject;

        Update() {
             transform.position = subject.position;
        }
 }

It simply sets the position of the camera rig to the position of its subject, but leaves the rotation as-is.

There are many ways to upgrade this basic script to make the camera movement smarter. You can use it to implement delayed or preemtive scrolling, enforce scroll boundaries, smooth out scrolling, switch the subject of the camera (preferably with a smooth pan instead of a sudden jump), add screenshake and many more.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks, mate. That's a really good protip. I will make my main camera follow the subject instead of mounting it. Wish me luck! \$\endgroup\$ May 15, 2020 at 12:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .