Shooting projectile from Touch.position Problem

I'm trying to instantiate bullets from a touch.position, but they end up in either world space instead of the screen/local space, or they will end up only spawning at the original spawn point.

ray = cam.ScreenPointToRay(Vector3(touch.position.x,touch.position.y,0));//cam.ScreenPointToRay (Vector3(Screen.width/2,Screen.height/2,0));
            Debug.DrawRay (ray.origin, ray.direction * rayLength, Color.yellow);

A separate script gives the bullets acceleration.

The top will display the origin and the farthest point the raycast goes. The code below will spawn the ball at the furthest point out, (which I don't want):

Instantiate(bulletObj, ray.direction *-rayLength, transform.rotation);

The next bit will shoot the bullets from the center of the raycast only:

Instantiate(bulletObj, ray.origin, transform.rotation);

How can I instance a bullet directly where I tap at the screen, or instance a bullet at the center and have it move towards the touch point?

Edit: I forgot to mention that I am using a perspective camera.

it looks like the ray you get is relative to the camera. so if you want to spawn bullets from the camera in that direction it seems like you simply have to add the camera's position to the ray origin.

ScreenPointToRay gives you a ray that starts at the 'screen point' that has been tapped. You should spawn your bullets at the camera's transform.position, and send them flying towards ray.origin.

What's the direction for, you wonder? Because we're dealing with 3D spaces, every point on the screen corresponds not to a single point in space, but to a line through 3D space - a sequence of points from very close to very far away. You know what the nearest point is - ray.origin - and all the other points on the line are found by starting at ray.origin and moving in ray.direction by varying amounts. Exactly what that direction is will depend on things like the FOV settings for the camera - the more narrow the FOV, the more 'straight ahead' the direction will be.

Note that the ray you get is not relative to the camera. From here:

Resulting ray is in world space, starting on the near plane of the camera and going through position's (x,y) pixel coordinates on the screen(x,y) pixel coordinates on the screen

"Resulting ray is in world space."

As superpig mentioned a ScreenPoint doesn't define an exact point in world space. To get an exact point you will have to specify the distance from the camera. The Camera.ScreenToWorldPoint function will calculate an exact world position for you if you provide with the screen point plus a z-value. Note that a z-value of 0 will return the transform.position of the camera. If you are wondering why, you could google for "view frustum".

You can use the Camera.mainCamera.ScreenToWorldPoint(touch.position) this will give you a vector3 value then instantiate your bullet

in your code would be like this

Vector3 myTouchInTheWorld = cam.ScreenToWorldPoint(touch.position);
//if you want to fix any value in Z could be done before the instantiation
myTouchInTheWorld = new Vector3(myTouchInTheWorld.x,myTouchInTheWorld.y, 0f);//value of choice
Object myNewBullet = Instantiate(bulletObj, myTouchInTheWorld, Quaternion.identity);
//if you want to fix the rotation
((GameObject)myNewBullet).transform.Rotate(new Vector3(0f,0f,0f));
//instead of 0's use the rotation desired

hope this helps you

first of all you should deside in what distance from the camrea, you want to create the bullets. then use ScreenToWorldPoint to find that post and then Instantiate it and move it toward the ray direction from that screen point. the code would be something like this.

temp = Input.mousePosition;
temp.z=5; //let's say we want to instantiate the object 5 meters away from the camera.
newPosition = mainCamera.ScreenToWorldPoint(temp);;
Instantiate (bulletObje,newPosition.transform.rotation); //rotation of the object with the script attached is passed and assumed that it is the camera.

then cast a ray from the same screen point and get it's direction and set the movement direction of your bullet to that direction.