I am currently working on an interactive game in Unity 3D where, upon pressing the "E" key, the player character interacts with an NPC. During this interaction, I aim to implement a camera zoom-in effect, focusing on the NPC, and restrict the player's ability to move the view until the NPC finishes talking.

The primary issues are:

  • Ensuring the camera consistently faces the NPC, irrespective of the player's initial orientation (The player can interact with the NPC while he is facing away from the NPC, side etc. He just has to be near the NPC).
  • Preventing the player from moving the view during the NPC's dialogue.
  • \$\begingroup\$ If you already have code for moving the camera, we'd need to see that code to be able to integrate this new behaviour with it. It would also be useful to describe or show the camera perspective your game currently uses (eg. first person, third person over-the-shoulder, etc.) \$\endgroup\$
    – DMGregory
    Jan 5 at 8:34
  • \$\begingroup\$ @DMGregory Alright. I will add the code right now. After that i'm going to check Philipp's solution since it sounds pretty good. \$\endgroup\$
    – Cezar P
    Jan 6 at 16:00

1 Answer 1


There are many ways to skin a cat. But my go-to solution for having a game that can be in different modes at the same time, like a "walk around" mode and a "talk to NPC" mode, is by creating a finite state machine that is simply implemented as a bunch of game objects, with each one representing a state and only one of them being active at the same time. Anything that is specific to only one state - input controller, game systems, UI elements etc. - are on that state's GameObject or its children. So a state transition can be done by simply deactivating the current game object and activating the new one. If you need any code to run when transitioning into or out of a state, put that code into the OnEnable and OnDisable event methods of scripts on those state objects.

Regarding the camera movement: I solved this problem in my last game using Cinemachine.

This optional package for Unity is a powerful camera controller that is particularly good at making fluent transitions from one camera perspective to another.

  1. If your project isn't using Cinemachine already, convert it so it does. Depending on what you already do in your project camera-wise that might require some small or large changes, so instead of going into greater detail here I would like to refer you to the available documentation and 3rd party tutorials which are plenty on the Internet.
  2. Create separate Cinemachine virtual camera objects. One "play mode camera" that shows the perspective your game is usually played from, and one "character closeup camera" for each NPC which you attach as a child to the NPC game object in a way that it always faces the face. Alternatively, you can also have only one single "character closeup camera" and control which NPC it shows using the "Position Control" and "Rotation Control" settings, and change which object it tracks via script at runtime (which fits particularly well into the "game states as GameObjects" pattern I mentioned earlier).
  3. When the game switches from "play mode" to "npc mode", deactivate the "play mode camera" and activate the "character closeup camera" with gameObject.SetActive. Or alternatively keep both active and control which one is active by canging their relative CinemaVirtualCamera.Priority property. To get back to play mode, do the same thing in reverse.
  4. You will now see that there is a nice, fluent transition between game view and NPC view, If you want to tweak this transition further, like make it faster or slower or change its easing, you can do so in the CinemachineBrain component.

You must log in to answer this question.

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