ScreenPointToWorldPoint not showing up as a field of Camera

:frowning:

you are trying to access it statically as part of the class, ScreenPointToWorldPoint exists on a instance of the camera.

Either get a reference to the camera you want to run it on, or use
Camera.main.ScreenPointToWorldPoint()

2 Likes

it’s not a field, it’s a non static function that takes parameters…

Oh ok. Thanks! I got it now. :slight_smile:

Thank you for the correction Lefty… ._.

Still not working. It still isn’t showing up as valid. The script I was editing was attached to the camera.

oops. ‘Camera.main’

lol im new to this

Also, in the future don’t put your code on some weird site I’ve never heard of and make me click on it. Just put it in your post, the forum has tools to format it for you.

1 Like

Ok can I see an example of how you’d move a gameobject to the mouse’ position in the game world? What I have now doesn’t work. I can’t even see the ball when the game starts and only the Y coordinate is changing…

Lol the ball just says frozen in the air. When I move my cursor it doesn’t follow it. It does it once and stays there. In the console it prints the exact same coordinates every single frame. I’d be nice if some one could help here.

This code acts like a constant. It’s way off from my mouse position too. And if I move my mouse it doesn’t move to it either.

Pos = Camera.GetComponent<Camera>().ScreenToWorldPoint(Input.mousePosition);
                Pos.z = gameObject.transform.position.z;
                gameObject.transform.position = Pos;

Input.mousePosition.z is always zero.You need to set the z-coordinate (in view space) before passing the screen position to ScreenToWorldPoint.

Stop using the forum as your debugging stream of consciousness. A single, concise post is enough to get assistance provided you wait longer than 20 minutes.

The z component in mousePosition is always 0. In ScreenToWorldPoint the Z position determines distance from the camera. So you’re getting a position 0 units away from the camera and then tacking an arbitrary Z value onto it which might not be accurate. Pick a distance from the camera and use it in the calculation

const float dist = 15;
var mPos = Input.mousePosition;
mPos.z = dist;
transform.position = camera.ScreenToWorldPoint(mPos);

Thanks for the replies guys. I sorta understood what you were saying, but it’s still not working. I play the game. Only the Y coordinate is changing, the ball is gone, can’t see it in sight. Is it cause I need more colliders so the ScreenToWorldPoint method works correctly? Idk, first time using this method. So not sure. :confused:

Colliders have nothing to do with using ScreenToWorldPoint. Hard to tell what’s happening without seeing code.

Let me further specify. I want to have the bowling ball move to the mouse’ position. But I’ll modify it to only move 1 axis so it moves left-to-right. But right now it’s not moving at all accurately. The bowling ball starts out near the foul line of the lane. When I play the game it immediately disappears. It appears no where near my mouse position. It should though, right? I tried directly using the code you posted in an Update method. Should Kinematic on the Rigidbody be set to true? Not sure what else to specify… I just want the ball to move directly where my mouse is pointing to in the worldspace. It’s not doing that right now.

Yes.

Didn’t make any difference. I even disabled the bowling alley mesh and I saw it out in the distance. But when I moved my mouse it didn’t move at all. I’m gonna remove the conditional statement and try it every frame instead. Why is this so hard?! :frowning:

It works! I see it moving almost perfectly out in the distance. I’m gonna change the Z coordinate now. Thanks guys for your help! I might modify this comment if I need further assistance.