0
\$\begingroup\$

How to use functions like ScreenToRayPoint() in Camera class?

I have used this code:

Ray ray = camera.ScreenPointToRay(Input.mousePosition);

and it says

Component.camera is obsolete, instead using GetComponent()

Then I tried these:

Camera cam = GetComponent<Camera>();

Ray ray = cam.ScreenToRayPoint(Input.mousePosition);

and it says

Camera doesn't contain a definition of ScreenPointToRay and no extension method 'ScreenPointToRay' accepting a first argument of type 'Camera' could be found.

\$\endgroup\$
0
\$\begingroup\$

Your code should work by the way. I think code you mentioned here is not exactly the same you used.

You did use Camera.ScreenToRayPoint(Input.mousePosition); instead of cam.ScreenToRayPoint(Input.mousePosition);

cam.ScreenToRayPoint(Input.mousePosition); should work without any doubt.

You can also use Camera.main.ScreenPointToRay from anywhere. But be sure that your Main Camera has tagged with "MainCamera"

\$\endgroup\$
  • \$\begingroup\$ Do I need to import the namespace for Camera to work? \$\endgroup\$ – Fatty Mieo Mar 5 '16 at 11:29
  • \$\begingroup\$ Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); > Camera does not contain a definition for 'main' \$\endgroup\$ – Fatty Mieo Mar 5 '16 at 11:31
0
\$\begingroup\$

I have a script named Camera, that's why I get the error(overrode UnityEngine.Camera), thanks for all the helping!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.