- Home /
Cinemachine: ScreenPointToRay raycast
I'm trying to integrate Cinemachine into an existing project, and I hit a snag when trying to Raycast from a screen point. It seems to cast the ray from an orientation opposite to the starting orientation. It also never changes that orientation even if the camera rotates in both the scene and game view and renders properly.
public RaycastHit findMouseOverPoint()
{
Ray ray = cam.GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Physics.SphereCast(ray, 0.5f, out hit, 1000.0f, pointerMask);
mouseOverHit = hit;
return hit;
}
void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawSphere(mouseOverHit.point, 1);
}
Interestingly, casting in OnDrawGizmos works properly in game view.
$$anonymous$$ake sure that you're running this script on the correct camera (that GetComponent returns the camera that you are refering to and not another one).
Answer by thieum · Nov 16, 2018 at 01:39 PM
I didn't tested your code, but note that CinemachineBrain (on your actual camera) has several update modes: LateUpdate, FixedUpdate, (smart?) Update. I solved a similar raycast issue (wrong camera.main position input) by having both CMBrain and my raycast function in the same pass (LateUpdate, for instance).
Hope it helps.
I changed my camera to Fixed Update for both Update $$anonymous$$ethod and Blend Update $$anonymous$$ethod, however not my camera doesn't seem to be driven anymore. I can see the C$$anonymous$$ vcam1 moving, however, my main camera isn't following.
Answer by Paladin-Masayume · Dec 01, 2018 at 08:24 PM
The problem in my case was another script that I use for the camera shake effect. Apparently, it changes the position of the camera in between frames sometimes and cinemachine takes that position. Sometimes the position is not off. Still might have something to do with those update rates you mentioned on the brain.
Your answer
Follow this Question
Related Questions
Why is the camera.screenpointtoray off? 1 Answer
Alternative to ScreenPointToRay needed 1 Answer
Follow mouse cursor (with Y = 0)? 1 Answer
Get 2D Collider with 3D Ray 2 Answers