- Home /
Problem Using ScreentoWorldPoint to Fire a Projectile
I am making game where a ball projectile is fired from the main camera towards the current mouse position. The ball is a Rididbody using gravity. Here is the code I have at the moment:
function BallThrow(){
Debug.Log ("Inside BallThrow function");
mouseX = Input.mousePosition.x;
mouseY = Input.mousePosition.y;
currentMousePosition = Camera.main.ScreenToWorldPoint(Vector3(mouseX, mouseY, -50));
ballDirection = currentMousePosition - Camera.main.transform.position;
var spawnedBall = Instantiate(ball, transform.position, transform.rotation);
spawnedBall.velocity = transform.TransformDirection(ballDirection);
}
Everything works as I want it to apart from the fact that the Y axis seems to be upside down. By that I mean that the mouse cursor position being higher results in the ball being thrown lower and vice versa. If anyone could shed any light on why this is the case I would be very grateful :) Thanks in advance for any help!
Answer by Bunny83 · Oct 24, 2012 at 09:09 PM
You transform a point which is outside of the view frustum of the camera. Everything that's before the near-clipping-plane might result in strange values. The perspective projection is only valid within it's frustum. So either use a positive number (not -50) as z value, or use Camera.main.ScreenPointToRay. The returned Ray already has a normalized direction
Thanks for this. I turned the camera object around 180 degrees and set the -50 value to 50 and now the Y axis problem is fixed :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613082826im_/https://answers.unity.com/themes/thub/images/avi.jpg)