Search Unity

VR Follow Player (car)

Discussion in 'Timeline' started by JamesWjRose, Oct 13, 2017.

  1. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    687
    I was pointed to Cinemachine as I am creating a VR race game and I need to follow the player's car from behind and above. Since I am new to Unity I am finding out things as I go, for example I found that when I set the Player to VR follow scripts do not work, that the camera has to be attached to the player object (in this case a car) and in doing so the camera shakes with every bump (yea, no. Let's all get sick!) so I started looking for a script/asset that will give me a more steady (steadi-cam?) solution.

    I downloaded Cinemachine, added a CM Virtual Camera, set the "Look At" and "Follow" to the car, that worked fine, but when i set the project to VR it does not follow. This example is with the camera NOT a child of the car (to avoid the bumps)

    Before I dig in further I thought I should ask if what my needs are can be handled by this asset.

    Thanks kindly for your time.
    James
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    If I understand correctly, you want the player to follow the car, but in a smooth way, i.e. minus the bumps. As if flying behind in a helicopter. Is that right?
     
    alexchesser likes this.
  3. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    687

    That is correct. However, since the car is on a road (actually a hover car, but it still follows the terrain's height) the camera cannot simply be at a fixed level. Right? (feel free to tell me I'm stupid and missing something)

    Have a great weekend
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    @JamesWjRose You're not being stupid - you want the VR camera to follow, but in a smooth way.

    Let's break this down into 2 steps:
    1. Make a helicopter-style follow cam with Cinemachine
    2. Put the VR headset inside the follow rig
    1. Helicopter follow cam.
    Cinemachine can do this. Make a virtual camera, and give it lots of damping on the body (see Cinemachine/Examples/Scenes/FollowCam for an example of this kind of vcam). This will significantly damp out the bumps. If necessary, you can smooth it out even more by creating an invisible object whose Update places itself at a smoothed version of the vehicle's position (a simple average of the positions within the last n frames would probably work just fine). Have your vcam follow the invisible object instead of the vehicle.

    2. Put the VR headset inside the follow rig
    That's a little tricky, because Cinemachine and VR are both trying to drive the Camera simultaneously - they will fight. You'll have to trick them into working together.

    Here's the procedure:
    1. Give them each separate Cameras to control. The VR camera is the real one, and Cinemachine will have a fake one. When you create a virtual camera, Cinemachine looks in the scene for a Camera, and automatically adds a CinemachineBrain component to it. In a VR scene, that brain will be added to the VR camera. You don't want that. You can prevent it from happening by first creating an empty game object, and manually adding a CinemachineBrain component to it. Once that is present, Cinemachine will not try to create any more Brains automatically. Delete any other CinemachineBrain components that may be already present in your scene.
    2. Disable the Cinemachine Brain's Camera component. You can't literally do that, or Cinemachine will stop working (this will change in future versions of Cinemachine) but you can get close: In the fake camera object, remove all the components except Camera and CinmachineBrain. Set the Camera's viewport to have zero size.
    3. Use the CinemachineBrain's game object as a container for the VR headset: make the headset a child of the CinemachineBrain camera object.
    I've made a little package to demonstrate this. It's just the FollowCam example, with some (rather extreme) bumps added to the vehicle, and the fake CinemachineBrain camera driving an ordinary camera, which you can replace with a VR headset. Install it in an empty project, and run the scene.

    EDIT: Cinemachine 2.1.09 now supports disabled Unity Cameras, so you don't have to do that zero-size viewport thing.

    ANOTHER EDIT: CM no longer requires that the Brain be attached to a Camera. You can attach it to anything.

    Let me know if this helps.
     

    Attached Files:

    Last edited: Feb 7, 2020
    Proto-G, alexchesser, Alverik and 3 others like this.
  5. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    687
    I'll give this a try and see what I can see. I'll come back with details.

    Thank you very much.

    EDIT: Sadly, this does not work. If I run the "VR Follow" scene without selecting "Virtual Reality Supported" then the Game window is working like you said with the camera following the two cubes, however when I set that checkbox the camera stays static.

    I'll look into it further, however I recall reading a few days ago that follow scripts don't work with VR/Oculus (however, i could be wrong about this)
     
    Last edited: Oct 16, 2017
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Yes, I didn't try it with VR Supported enabled. What I was trying to show is that you can use Cinemachine to control a container for the actual camera. AFAIK the correct way to "move" the VR headset is to place it in a container object, and move that. So really this should work. I'll give it a try tomorrow with a VR setup (have to change computers to try that).
     
    Last edited: Oct 29, 2018
    Alverik and JamesWjRose like this.
  7. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    687

    No, no, I will try that and get back to you. Thanks for the clarification and your time.
     
  8. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    687
    You are correct about that, and it is a GREAT help. I am tweaking things but at least now I have a start. Thank you very much
     
    Gregoryl likes this.
  9. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    687
    if anyone cares, I Frankenstein'd some code together for a simple VR Camera/Player Follow script. Details are here: https://github.com/Blissgig/VR-Camera-Follow

    This is without using Cinemachine, though I am sure if someone were to add it's capabilities it would improve the script. I just wanted simple starting point.

    Thanks again Gregoryl, the script on an Empty Object was the point needed to create this.
     
  10. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    hmmm...I need to do this now and can't get it working with cinemachine.
    My use case is a follow cam on a bird, using cinemachine to switch perspectives on the fly between two virtual cinemachine cameras.

    But I want the headset position to be moved by cinemachine, but rotation can be controlled by the user movement. So cinemachine tells the "story" by position transitions, but the user still gets a measure of control by being able to look around. Make sense?

    Any clues on how to get this working?
     
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    @sonicviz Put the headset in a container object, and attach the CinemachineBrain to that, as detailed in my post earlier in this thread. Then make two vcams, each following the bird in a different way. The vcams will effectively drive the VR headset container, and since the headset is in the container, the player will be able to look around.
     
  12. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    That works, ty!
     
  13. Proto-G

    Proto-G

    Joined:
    Nov 22, 2014
    Posts:
    213
    This is what I needed and more! Thanks!
     
    JamesWjRose likes this.
  14. Mingzilla

    Mingzilla

    Joined:
    Mar 31, 2022
    Posts:
    1
    This is very helpful. However, I'm struggling trying to understand what you mean by "headset". I guess "headset" means the gameobject that has the "Camera" component. The problem is, in the game, when I turn my head, the whole screen moves, not the camera.

    I have:
    * CinemachineBrain - An empty gameobject XX, which has the CinemachineBrain component
    * Camera - The gameobject XX has a child YY, which has the Camera component (is I guess this is the "headset")
    * VirtualCamera - gameobject XX has a sibling object ZZ, ZZ has the CinemachineVirtualCamera component, which follows the player

    I understand the trick is to make sure
    * the container of the VR headset (I guess it's the camera but I can be wrong) follows the the player
    * the VR headset is inside the container, which can rotate freely without fighting against cinemachine to handle rotation

    However, regardless if I create this parent child structure, or having CinemachineBrain and Camera both on the same gameobject, the results are the same, turning my head moves the whole screen, not changing the angle of what I'm looking at.

    Unity version: 2021.3.3f1

    Any help would be super appreciated.
     
  15. pint81287

    pint81287

    Joined:
    Feb 21, 2024
    Posts:
    1
    there is an error in the scripts