Search Unity

Some Questions Regarding Using The Firstpersoncontroller And Cm

Discussion in 'Cinemachine' started by f0ff886f, Apr 13, 2019.

  1. f0ff886f

    f0ff886f

    Joined:
    Nov 1, 2015
    Posts:
    201
    Hi, first of all, thank you for CineMachine. My brief time to play with it has left some positive impressions.

    I may have some noob questions here, if there's a better resource to get answers than asking please point the way. I followed the steps here: https://forum.unity.com/threads/creating-a-pov-camera.505429/ and used approach #2 to create a Virtual Camera on my child object that is rotated by the FirstPersonController script. So far so good, for basic control.

    Some notes on the POV approach in #1: While I got mouselook to work, I don't understand how to couple the rotation of my main Player object so that you walk where you look (do I have to write a simple rotation script based on the mouse input in that case?).

    Also, the mouselook is weird a bit, in that it seems to take the axis value from but not the amount of motion in the axis... Its hard to put in words but I'll try to explain: in a normal FPS, if I move my mouse 10cm from left to right, lets say my character will do a full 360* spin. If I move my camera slowly 10cm from left to right, my character does the same spin, just slower.

    With the POV module, if I move my mouse 10cm but very fast, my rotation will only occur based on how long the mouse axis was sending a value >0 (which in this case will be a short amount since I moved the mouse quickly). Likewise, if I keep moving the mouse slowly, the rotation will occur at the max pace. So, in general, this is not what I want, I'd rather keep the traditional FPS feel.

    Now, onto things I don't understand :)

    The big draw of CineMachine for me is that I want to be able to do things like force the player to focus on an object for a short amount of time, but not to take away the control of their movement altogether (or take away the movement of the player). I've been playing with multiple virtual cameras. As I understand it, my main camera should never move and just live somewhere in the world, and I always render from the perspective of a virtual camera.

    So my one "main" virtual camera is on the Character's head gameobject, and that's fine. Now, how do I force a look at, but influence the rotation of the character controller's transform so its a whole body look at, and not just a head look at? So the character controller should orbit the lookat target. Is this something CineMachine can help with? This is probably answering how to use the POV module with the main player object from above :D

    Secondly, if I want to animate the player's body + camera for a cutscene for example, I would go to Timelines for this. Now, unfortunately I haven't figured out how to animate a simple component enabled/disabled state (so that I can turn off the character controller completely for the movement sequence) in Timelines, but I guess thats a different problem for a different thread. However, for the purpose of the animation, I would still need to animate the perspective of the camera. Would I create a second virtual camera, but parented to the player object (so vcam #2 is a sibling to my main fpc vcam), and then just animate that one however I like? Then when I turn off the 2nd vcam, the main vcam should blend to the rotation of the 2nd vcam, so the player can resume from the new position?

    I'm sorry this is a wall of text! I am one toe in the water with CineMachine, I feel it would make my life easier... although the lack of traditional events in Timelines will always give me pause whether or not I can be adopted by our new CM and Timeline overlords...
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    About the POV weirdness. The default behaviour of the input axes uses the concept of maximum speed, and prevents the camera from rotating too fast. This is at the root of the behaviour you don't like.

    You can override the default axis behaviour by disabling the POV's input axes and driving the axis values yourself. I've attached a couple of scripts as an example of this. Drop them into your project, and add the PovAxisDriver behaviour to your POV vcam. I think this will give you the sort of movement that you're looking for. You can tune it for sensitivity.
     

    Attached Files:

  3. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    Yes, that's correct. You animate the vcams, and let the main camera position itself according to the active vcam.

    I think I need to better understand what you're trying to do. So, the player is using the the POV camera, and then you want to force him to look at something, but while that is happening, if he moves the mouse, you want the character's position to orbit around the lookAt target, while always looking at the same spot?

    If so, then you're looking at some custom code to move the controller around when you're in that mode. Also, you would enable a new vcam (a sibling of the "main" vcam, inside the controller) with Do Nothing in the Body and a Composer in the Aim, with the LookAt target set to your target.

    An alternate strategy you might try is to create a FreeLook camera, with LookAt and Follow set to the target in question, and the orbits dynamically set to match the controller's distance from target. You would need some custom code to position the controller according to the FreeLook and to disable the normal controller behaviour. If you just want a horizontal orbit (no vertical movement) then you can replace the FreeLook with an ordinary vcam having an OrbitalTransposer in the body. Then only 1 orbit to worry about.

    You can use an activation track in timeline to activate/deactivate objects

    If it's a cutscene then you can just place the second camera anywhere (no need to have it inside the controller), and the main Camera will blend to that position when you enable the vcam, and back out to the normal vcam when you disable it. While the cutscene is running you can just reposition the controller to be where you want it.
     
  4. f0ff886f

    f0ff886f

    Joined:
    Nov 1, 2015
    Posts:
    201
    Sorry, I will try to make some graphics later to explain this better, but for now words will be my attempt :)

    For the overall case of enabling the POV camera:

    Its a first person game. The way the FPC works now is the traditional Unity way: rotate around Y axis for the object containing the character controller, rotate around X for camera (I guess to avoid gimbal lock?). That way when I move the mouse, the actual orientation of the character on the XZ plane is spinning, and holding W to walk forward means I'm always walking in the direction I move.

    If I switch to POV camera, I need to still drive the orientation of the original character controller (right now there is a dis-syncrhonization so you walk always in the forward vector of the character controller if I use POV camera). So in the new Cinemachine world, when Cinemachine controls the entire rotation on the camera (around both axis), how can I rotate my player controller so that it still works? Should I read the value from virtual camera rotation, apply the rotation to the character controller and reset the camera animation to always be 0 in the Y axis? That's essentially what I need: to translate the Y axis rotation from the camera to the Character Controller.

    If I get the above working, then the cutscene case I describe will always work: Cinemachine will drive the camera rotation, I will apply the Y axis rotation to the character controller, and that way when the player hits WSAD, the forward vector of the character is always looking at the LookAt target, so using the movement keys the player will naturally always orbit the LookAt target.

    Does this help?

    The more I write it the simpler the problem becomes in my mind: how can I rotate the parent GameObject of the virtual camera so that it reflects intended the Y-axis rotation of the virtual camera?
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    Quite often problems become simpler after you describe them :)
    Here is a question: why switch to POV controller? Why not just keep the existing FPS using a vcam instead of a Camera? Then everything just works.
     
  6. f0ff886f

    f0ff886f

    Joined:
    Nov 1, 2015
    Posts:
    201
    Yes, I think we derailed a bit to focus on POV too much :) I would rather switch to just a vcam. I still have one question with this though, the fundamental one, how to rotate my character controller after CineMachine resolves its own rotation

    Something like this in the pseudoflow below:

    Code (csharp):
    1.  
    2. Assume rotation offset around Y between Character Controller and VCam Child GameObject is 0 (vcam local rotation = (x,0,z))
    3.  
    4. 1) Move Character Controller
    5. 2) Update Virtual Camera Which is Looking At A Target from the position in 1), Virtual Camera now has a Y-axis rotation
    6. 3) Character Controller.rotation.y += VirtualCamera.rotation.y
    7. 4) VirtualCamera.rotation.y = 0
    8.  
     
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    I don't understand why 2) is a problem. If you just have a passive vcam parented to the controller, and then move the controller, the vcam will move along with it. That's generally how FPS games are done. What do you want to do that's different?
     
    mwayy likes this.
  8. f0ff886f

    f0ff886f

    Joined:
    Nov 1, 2015
    Posts:
    201
    Well CineMachine has very nice tracking algorithms that let you llook at a target, or allow the target to move a bit in some margin of error in the frame to make things more natural, or add some noise function etc. I want to benefit from that motion in cinematic sequences, without losing control of the player's body.

    So Cinemachine is driving my LookAt, and I read back the forward vector Y-rotation from Cinemachine in time to still apply that rotation to the character's body before the frame ends.

    I really need to make this a video or something, I will try to do that this evening.
     
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    Either it's FPS or it's not.

    In FPS mode, user is controlling where the camera looks and stands, via the FPS controller. There is no tracking. If at some point you want to switch to non-FPS mode (perhaps for a cutscene), with the camera driven procedurally using tracking, that's fine.

    The paradigm to understand with Cinemachine is that you shouldn't try to do everything with the same vcam. In FPS mode, you have your passive vcam. In cutscene mode, you enable new vcams with different behaviours, which take over control of the camera. When the cutscene is finished, control is reverted back to FPS passive vcam.

    Cinemachine gives you control over how the transitions between vcams are made. They can be cuts or blends. With blending, you can create very complex camera behaviours by blending between simple vcams, each designed to do a simple thing. That's the magic.

    Only the FPS vcam is parented to the controller. The other ones can be anywhere. The main camera (with the CM brain) will figure out which one to track and how to get there.
     
    mwayy likes this.
  10. Darkon_Who

    Darkon_Who

    Joined:
    Jul 22, 2017
    Posts:
    14
    Hey @Gregoryl I am still struggling with the issue above explained as below:

    `Also, the mouselook is weird a bit, in that it seems to take the axis value from but not the amount of motion in the axis... Its hard to put in words but I'll try to explain: in a normal FPS, if I move my mouse 10cm from left to right, lets say my character will do a full 360* spin. If I move my camera slowly 10cm from left to right, my character does the same spin, just slower.

    With the POV module, if I move my mouse 10cm but very fast, my rotation will only occur based on how long the mouse axis was sending a value >0 (which in this case will be a short amount since I moved the mouse quickly). Likewise, if I keep moving the mouse slowly, the rotation will occur at the max pace. So, in general, this is not what I want, I'd rather keep the traditional FPS feel.`

    This is extremely noticeable when trying to use touch input. Ive tried the scripts you input above, but i still get the exact same results. Is there any way you can please help with this? A good idea is to look at how PUBG / Call of duty mobile are doing it, basically i cant do touch flicks with the current Cinemachine PoV setup.

    Please help! Its the last thing needed to make my aiming great for my mobile game
     
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    @Darkon_Who The Axis can be driven in 2 modes: speed-limited (the default), and direct (what you want). For direct mode, set each axis to Input Value Gain, like this:

    upload_2020-5-26_7-1-56.png

    (you will have to decrease the Speed setting considerably when you do this, as it is interpreted differently in this mode)
     
  12. Darkon_Who

    Darkon_Who

    Joined:
    Jul 22, 2017
    Posts:
    14
    Hey @
    Thank you so much for the reply. So will the speed value (while under input value gain) be used as the sensitivity multiplier then?
     
  13. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    yes, exactly
     
  14. Darkon_Who

    Darkon_Who

    Joined:
    Jul 22, 2017
    Posts:
    14
    Hey @Gregoryl Thank you very much for the replies.

    I have made these changes and it does seem to feel better and snappier.

    Thank you very much for being willing to help!
     
    Gregoryl likes this.