Search Unity

Raycasting with virtual cameras

Discussion in 'Cinemachine' started by LilyMcCheddar, Mar 19, 2018.

  1. LilyMcCheddar

    LilyMcCheddar

    Joined:
    Feb 4, 2017
    Posts:
    10
    It's me again, desperately trying to integrate virtual cameras in my first person game.
    I have been using a cinemachine POV camera because I couldn't figure out any other way to efficiently clamp and "slow down" a regular camera and get the behaviour I wanted.

    http://gph.is/2IsNzby

    Now I have problems making my system work because I can't figure out how to get raycasting to work with the virtual camera. Am I going with the wrong approach here ?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    I'm not quite sure I understand your problem. The virtual camera is just puppeteering the Camera, controlling its transform. Just do raycasting from the main camera, as you normally would. Maybe you could elaborate a little on that the problem is?
     
  3. volleygo

    volleygo

    Joined:
    Dec 6, 2017
    Posts:
    2
    I have the same problem in my project. I used a virtual camera with timescale slow down and the raycasting doesn't work.o_O
    However, I tested with main camera and timescale down, the raycasting work normally.;)
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    In Cinemachine, the Camera's transform gets updated in LateUpdate, so if you're trying to raycast with it, make sure you're doing it after it has been updated. Check the Script Execution order, and make sure your script gets called after CinemachingBrain, which is where the update happens.
     
  5. metroidsnes

    metroidsnes

    Joined:
    Jan 5, 2014
    Posts:
    67
    I'm using Unity 2018.2.0f2 with CM 2.2.2 and in the Script Execution Order window, in the dropdown I can't see any Cinemachine script. If CM scripts doesn't appear there, how can I make sure that my scripts execute before (or after) Virtual Camera gets updated on the scene?
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    The Package Manager currently doesn't expose package scripts in the ExecutionOrder inspector, and so there is no way to manipulate them without embedding the asset into your project. This is a known issue and will be addressed in future versions of Unity.

    However, the execution order is contained in the .meta file for the CM brain, and it you look at it you will see this:
    executionOrder: 100
    so if you set the execution order of your script to something greater than that you should be ok.
     
    metroidsnes likes this.
  7. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    I'd love it if the virtual cameras had the raycast functions on them. sometimes you want to know if the 'main gameplay view' could see something if the rendering camera was aligned with is while cinemachine has blended the rendering camera somewhere else.

    At the moment you need to attach a camera to any virtual camera you want to cast rays from, that kinda seems redundant to me.
     
    forteller, randyhuynh and LucieSaad like this.
  8. uzisho

    uzisho

    Joined:
    Jul 3, 2019
    Posts:
    14
    POSSIBLE SOLUTION FOR ANYONE READING THIS
    I've been butting my head against that the entire day.
    You need to have the raycast happen in FIXED UPDATE.
    Also - you have to make sure that the raycast script is running at the same time as the cinemachine script.
    upload_2021-3-17_14-59-10.png
     
    tsibiski likes this.
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Another possibility is to do the raycasting after the CM Brain has positioned the camera. You can do this in an event handler for
    CinemachineCore.CameraUpdatedEvent