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:
    19
    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:
    2,501
    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:
    19
    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:
    2,501
    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:
    19
    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.