- Home /
raycast question
Hello
I would like to fire a projectile, from the player towards a point pressed by the user in a sidescrolling 3d game.
I already got to the part, where I can draw a line, from the player to the point the user touched on the screen, but I can't translate that screen point to world coordinates. AS far as I know, it should be something like ScreenToWorldPoint, but it gives weird results.
Here is my code:
private void Shoot ()
{
ray = camera.ScreenPointToRay (Input.touches [0].position);
if (Physics.Raycast (ray, out hit, range)) {
Debug.Log (string.Format ("Hit: {0}!", hit.transform.gameObject.name));
} else {
Debug.Log ("Missed!");
}
Vector3 targetPoint = ray.GetPoint (range);
targetPoint.z = 0; //sidescrolling game
Debug.Log ("targetPoint:"+targetPoint);
Debug.DrawLine (gameObject.transform.position, targetPoint, Color.red); //this part seems to be ok
Vector3 targetWorldPoint = camera.ScreenToWorldPoint(targetPoint);//this is wrong, it always give me strange results, example: targetWorldPoint:(0.0, 1.0, -18.8)
Debug.Log ("targetWorldPoint:"+targetWorldPoint);
}
Help would be greatly appreciated.
Answer by Seth-Bergman · Dec 28, 2012 at 01:53 PM
like this:
private void Shoot ()
{
ray = camera.ScreenPointToRay (Input.touches [0].position);
if (Physics.Raycast (ray, out hit)) { //using a "range" var makes no sense here
Debug.Log (string.Format ("Hit: {0}!", hit.transform.gameObject.name));
} else
Debug.Log ("Missed!");
Vector3 targetPoint = hit.point; //this is the world point
Debug.Log ("targetPoint:"+targetPoint);
Debug.DrawLine (player.transform.position, targetPoint, Color.red);
}
using a third var, in your case "range", is to limit the distance of the raycast. for example, if I pass in range with a value of 10, the raycast will only be 10 units long.. This of course makes no sense with a ScreenPointToRay in a sidescroller...
EDIT:
I changed the line to go from the PLAYER, rather than the CAMERA. make sure "player" references the correct object! DO NOT BOTHER ADJUSTING Z POSITIONS (unless you are actually setting the ACTUAL POSITION, such as "player.transform.position.z = 0;" for example, it makes NO SENSE!!!)
It's a sidescrolling game. I doubt raycast from camera will give good results as it would ignore all obstacles from player to target.
Edit: Ah you meintoned that. Well he should use ScreenToWorldPoint and then cast ray from player position to that point.
Debug.DrawLine (gameObject.transform.position, targetPoint, Color.red);
This does not seem to work, it only draws it to the right side of my player. Oo I have a perspective camera btw. Does that matter?
no... but I just noticed, is this script attached to your camera?
if so,to draw a line from the PLAYER, you would need to do:
Debug.DrawLine (player.gameObject.transform.position, targetPoint, Color.red);
also, part of the issue could be using the z value at 0, if you have rotated your camera from its default position.. (or if your player has a z position other than zero)
there is probably no need for the line:
targetPoint.z = 0;
to see better what exactly is going wrong, try PAUSING the game with the line in view, and rotate around in the scene view to see exactly where it is...
The camera is perpendicular to the playfield. The script is attached to the player. Drawing the debug line from the player to the touch point worked fine in my original code, but I can't fire a projectile from the player to that point. So I would want to do something like this but I don't know how:
GameObject go = (GameObject) Instantiate(basicProjectile); go.rigidbody.AddForce(X); //X what to do here? How do I fire the projectile from the player, to the touch point?
go.rigidbody.AddForce(targetPoint-player.transform.position);
but judging by the code your original code was way off, it may have APPEARED right from your perspective..
try losing the line:
targetPoint.z = 0;
Your answer
![](https://koobas.hobune.stream/wayback/20220613090034im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to touch select 3D objects 2 Answers
Raycasting freezes the Android device? 2 Answers
Touch Object Android/IOS 0 Answers
Using raycasts to move a game object toward touch point. 0 Answers
Problem with Vectrosity line colliders 0 Answers