Screen point to "real" world position?

+1 vote
asked Jan 6, 2016 in Programming by JD Student (300 points)

I have search thru the complete unity anwser forum, tried all the snippets and nothing worked as expected!

All solutions gave me the position of the camera which is obviously wrong!!

Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane));

Can someone here please give me a working solution?


2 Answers

+1 vote
answered Jan 6, 2016 by Bugstacker Senior (4,800 points)
selected Jan 6, 2016 by JD
Best answer

Cast a ray through the camera and strech it by the desired distance.

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

float distance = Camera.main.nearClipPlane;
Vector3 worldPos =  Camera.main.position + ray.direction * distance;




0 votes
answered Jan 6, 2016 by Gleny Student (480 points)
Your snippet should actually work. I see nothing wrong with it.
Welcome to Bugstacker Q&A, where you can ask questions concerning Game Development, Programming and Design and receive answers from other members of the community.

Help for