Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Top-down shooter camera sliding towards mouse

Discussion in 'Cinemachine' started by Mystic_Quest, Oct 17, 2018.

  1. Mystic_Quest

    Mystic_Quest

    Joined:
    Feb 22, 2016
    Posts:
    47
    Returned to Unity after some time and been toying with Cinemachine a bit. Got a top-down 3d shooter (60 deg perspective) with the player aiming at the mouse. Added a virtual cinemachine cam for gameplay (not even sure if it was the right one to use), set it to follow and aim the player, adjusted some settings so that the camera doesn't rotate at all and I got a working camera.

    Now I want to make it slide towards/follow the mouse position (where I am aiming at) up to a point, like in many top-down shooter games and have a deadzone near the character to avoid small camera movements. Any idea if that's possible with cinemachine? Don't know where to start.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    Hey @Mystic_Quest Sorry for the long delay. This post fell off my radar and I just noticed it again.

    The Framing Transposer is designed primarily for 2D use, and as such isn't perfect for the situation you describe. That said, it is possible to get pretty close, with a little tinkering. I tried it out, and am including my results in the attached package. Create a new project, add Cinemachine from packman, then drop this in and open the scene. When you play it by using WASD to move the player around, the camera follows at a more-or-less constant height, with a dead zone in the center. That's what you're looking for, right?

    Here is how I set it up. You have to first set up your vcam with the Framing Transposer and the viewing angle (60 degrees, in this case). Then, put in the desired camera distance, and the dead zone height.

    Next, run the scene. When you move the player back and forth, the camera will move up and down in Y to maintain the target distance. You don't want that, so here is where the tinkering comes in. You have to set the dead zone depth just right to neutralize the vertical movement - and that amount will depend on the specific camera distance, angle of view, and dead zone height.

    upload_2018-10-29_14-35-28.png

    What I did was to move the player back and forth so that it reached the top and bottom of the dead zone, and keep an eye on the camera Y position in the inspector. I then gradually adjusted the dead zone depth until the Y movement disappeared, or was sufficiently negligible. And that was the value I kept.
     

    Attached Files:

  3. Mystic_Quest

    Mystic_Quest

    Joined:
    Feb 22, 2016
    Posts:
    47
    Hello, no problem and thanks for the reply!

    Ah it seems the framing transposer was in 2.1! Still had 2.0. At any case, was looking for a way to handle the camera only with cinemachine without any scripting and was looking for a way for it to "gravitate" towards the mouse cursor when it gets far enough from the player.

    Managed to do it with a bit of scripting (executable if you wish to see it: https://www.dropbox.com/s/zxsqwqnaiznpss5/test.rar?dl=0).
    Lerp'ed between raycast cursor and player vectors with 0.3 weight, placed an empty object for the vcam to follow.

    Only problem atm is that I wanted the camera's movement damping to happen only when I move it with the mouse (and also have a deadzone), not with the player movement, which is not possible the way I did it (or any other way that I know of). :p
     
    Gregoryl likes this.
  4. Dominiclh08

    Dominiclh08

    Joined:
    Feb 14, 2019
    Posts:
    1

    Any update or way to automate this? Because we are early on the development of our game, and our camera settings might change, and it doesn't feel right to have to ajust the dead zone depth everytime.

    I'm making a bullet-hell game, so camera Y position should never change. But I also want damping on the camera, which makes this fix unusable, as the Y position of the camera will now infinitly go up or down if I move up and down.
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    You can make a simple custom extension to lock the vcam's Y position. This works well with framing transposers, damping, and dead zones. Because the FramingTransposer was designed for a straight-on camera, the screen guides won't be completely accurate when the vcam is at an angle, but you can still adjust the settings to get the movement you need.

    Here is an example of a custom extension that locks the camera Y position. Drop it into your scene, and add it to the vcam from the Extensions dropdown in the inspector.
    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. /// <summary>
    5. /// An add-on module for Cinemachine Virtual Camera that locks the camera's Y co-ordinate
    6. /// </summary>
    7. [SaveDuringPlay] [AddComponentMenu("")] // Hide in menu
    8. public class LockCameraY : CinemachineExtension
    9. {
    10.     [Tooltip("Lock the camera's Y position to this value")]
    11.     public float m_YPosition = 10;
    12.  
    13.     protected override void PostPipelineStageCallback(
    14.         CinemachineVirtualCameraBase vcam,
    15.         CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    16.     {
    17.         if (stage == CinemachineCore.Stage.Finalize)
    18.         {
    19.             var pos = state.RawPosition;
    20.             pos.y = m_YPosition;
    21.             state.RawPosition = pos;
    22.         }
    23.     }
    24. }
    25.