Search Unity

With Cinemachine RaycastHit not working for Player to Click to Move

Discussion in 'Cinemachine' started by nlv22, Jan 11, 2019.

  1. nlv22

    nlv22

    Joined:
    Dec 20, 2016
    Posts:
    31
    I have seen some versions of this problem being asked but I cannot get this to work. I read about using 'LateUpdate' but it did not do anything.

    Before I added a Cinemachine Virtual Camera, my player click to move/ interact script(using navmesh agent) was working fine. This is part of my code below where I am using RaycastHit.

    Code (CSharp):
    1.     void LateUpdate () {
    2.        
    3.         //Movement on the Ground unless we select an object
    4.         if (Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
    5.         {
    6.            
    7.             Ray ray2 = cam.ScreenPointToRay(Input.mousePosition);
    8.             RaycastHit hit2;
    9.            
    10.             //Movement if we select an item
    11.             if(Physics.Raycast(ray2, out hit2, 1000f, itemMask))
    12.             {
    13.                 Interactable interact = hit2.collider.GetComponent<Interactable>();
    14.                 if (interact != null)
    15.                 {
    16.                     SetAttention(interact);
    17.                 }
    18.  
    19.             }
    20.             else
    21.             {
    Code (CSharp):
    1.     //Movement if we select an item
    2.     void SetAttention(Interactable newAttention)
    3.     {
    4.         attention = newAttention;
    5.         _playerMovement.FollowTarget(newAttention);
    6.     }
    7.  
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,720
    You're saying that it works without a CM vcam, but not with? Very strange.
    Can you post an image of the vcam inspector?
    And the hierarchy, showing the main camera and the vcam and the target?
     
  3. nlv22

    nlv22

    Joined:
    Dec 20, 2016
    Posts:
    31
    I reverted everything back. Added Cinemachine again, checked every vcam setting as I added it to see what works. I narrowed down the problem:

    Under Vcam's Body section, the 'Camera Distance' does not work at 30, but when I bring it back to 10, it works. I would like to distance my camera back. Do I need to make changes to my script? Here are the screenshots:

    Capture0.jpg Capture1B.jpg
     
    Last edited: Jan 12, 2019
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,720
    Sorry for the delay.
    It would greatly help if you could package a lightweight scene that demonstrates this problem.
    If you could send that to me, I could have a look. It's hard to tell what's going on just from your description.
     
  5. nlv22

    nlv22

    Joined:
    Dec 20, 2016
    Posts:
    31
    Thanks

    While making a small lightweight version of the project, cinemachine and raycast with my player actually worked - so I am not quite sure what inbetween thing I added to my entire project to cause a problem.

    I found somewhat of a fix by checking on 'Physical Camera' for the Main Camera. The only thing that doesn't work is raycast for the UI as mine line 4 from the previous code. But I think I'll find another way for the UI part.

    Code (CSharp):
    1. if (Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
    What was confusing was that by switching the 'Camera Distance' under Cinemachine, it partly worked. However, I do not think is a cinemachine problem anymore.
     
    Gregoryl likes this.
  6. judohurley

    judohurley

    Joined:
    Feb 27, 2014
    Posts:
    8
    I know this is older, but I've run into the same issue I think. Using 2020.2.1f1 and CM 2.6.3.

    In my case I have a VCam using Orbital Transposer and Composer Aim. My follow offset is set at "0,10,-20" at start. I have a custom component that modifies the follow offset for zoom capabilities. (See below for context)
    Code (CSharp):
    1.  
    2.    
    3.     CinemachineVirtualCamera _vc;
    4.     CinemachineOrbitalTransposer _ot;
    5.     public float ZoomStep = 5;
    6.     public float MinZoom = 5;
    7.     public float MaxZoom = 25;
    8.  
    9. void Start() {
    10.     _vc = GetComponent<CinemachineVirtualCamera>();
    11.     _ot = _vc.GetCinemachineComponent<CinemachineOrbitalTransposer>();
    12. }
    13.  
    14. void Update() {
    15.     if (Input.mouseScrollDelta.y != 0)
    16.     {
    17.         float delta = (-Input.mouseScrollDelta.y * ZoomStep);
    18.         _ot.m_FollowOffset.z = Mathf.Clamp(_ot.m_FollowOffset.z - delta, -MaxZoom, -MinZoom);
    19.     }
    20. }
    21.  
    Ignore all the double and triple negatives (code still in concept stage)

    If I set my MaxZoom to 25 (which limits the Z >= -25) all my raycasts from screen work fine. However, if I set it to 30 (for example) and if I zoom out past 25, my raycasts no longer work. They work again when I zoom back <= 25.

    Raycast code below for context.
    Code (CSharp):
    1.  
    2. void Update() {
    3.     if (Input.GetMouseButtonDown(0))
    4.     {
    5.         RaycastHit aim;
    6.         Camera realcamera = Camera.main;
    7.         if (realcamera != null)
    8.         {
    9.             Ray ray = realcamera.ScreenPointToRay(Input.mousePosition);
    10.  
    11.             if (Physics.SphereCast(ray, 0.5f, out aim, 2000f))
    12.             {
    13.                 OnSelect?.Invoke(aim.transform, new EventArgs());
    14.             }
    15.         }
    16.     }
    17. }
    18.  
    Any ideas are appreciated. I will not put together a sample project file, as the code above is the only operative code in this situation. All other settings in CM for the brain and Vcam are defaults.
     
  7. novavillanueva

    novavillanueva

    Joined:
    Mar 11, 2013
    Posts:
    1
    I had forgotten about this post I made but I learned more about this problem and what it was for me after:

    1) It turned out that the ‘max cast’ which in yours is 2000f could be a large crazy number if the scene is two large or your very zoomed out.
    I think when I made a new scene - I use unity standard cube size to measure everything so mine scene wasnt that large.

    2) Another thing that I beleive was conflicting in my scene was that I had many layers including a post processing layer affecting the raycast. I would work withs layers if I where you but make sure they are not conflicting.

    3) And the one thing I mentioned above was that I enabled ‘physical camera’ in the main camera (not the vcam). This was done after, not shown on the screenshot.
     
    Gregoryl likes this.
  8. judohurley

    judohurley

    Joined:
    Feb 27, 2014
    Posts:
    8
    Thanks for the reply! Unfortunately it didn't directly fix my issue, but it did have a connection to my problem! I have a trigger capsule collider in my scene that detects when AIs leave the (circular) scene area... it's like my entire scene is in a barrel. When I zoom out past 25, my ray origin ends up outside the "barrel" because the camera is outside the collider. Because of that, the first thing the raycast hits is the "barrel" trigger collider, not the objects inside the "barrel." In case anyone needs a debugging trick, I found it by Debug.Log()ing the gameObject.name in my raycaster section for the collision. When I saw what was being detected, it was suddenly obvious.

    The fix, as you sorta mention in #2 is to use layers. I need to put the trigger in a layer and ignore that layer in my raycasts. So my problem was not enough layers! LOL

    So, thank you for your update!
     
    Gregoryl likes this.
  9. 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.
    View attachment 816926
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,720
    @uzisho Not at the same time. After. Use 101 or more as the execution order value.
     
  11. Jiams01

    Jiams01

    Joined:
    Apr 4, 2015
    Posts:
    3
    Hi, i just got this same issue. I'm using Cinemachine and unity 2020.3.16f1. And doing a simple raycast and tried a above solution but not working for me. execution order value 101 and more than Cinemachine brain have 100.
    also used a fixedupdate for raycast. What can be a issue?
     
  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,720
    Do your raycast in LateUpdate, not FixedUpdate.
     
  13. Jiams01

    Jiams01

    Joined:
    Apr 4, 2015
    Posts:
    3
    Thanks for your reply Gregoryl, I will try LateUpdate. well i have to used a temporary solution by MonoBehaviour.OnMouseDown().