How to shoot projectile towards touch position for mobile?
I am completely lost with touch position compared to world/screen space. All i would like to do it be able to touch on the screen and shoot a projectile from the player position (which is in the center of the screen with the camera a child of the player) towards the touched position for mobile application. Right now when I shoot the projectile, I have it going towards the touch position, but it goes in world space and disappears on the z axis. I've tried locking the z axis, I've tried changing the screen point, but nothing seems to work. Any help would be fantastic as it may be something simple that I am overlooking or not knowing about. Thank you for all help.
current instantiating projectile script here -
if (Input.touchCount > 0) {
if (Input.GetTouch (0).phase == TouchPhase.Began) {
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
GameObject bullet = Instantiate (projectile, transform.position, Quaternion.identity)as GameObject;
bullet.transform.LookAt (touchPosition);
}
}
current script attached to projectile -
transform.position += transform.forward * bulletSpeed * Time.deltaTime;
Answer by splitter20 · Nov 23, 2016 at 11:02 PM
I took a few things from the comments on the initial question and added some changes. Thanks for pointing me to the right direction to find the answer everyone. Here is the final script and it works perfectly.
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class Shooting : MonoBehaviour {
public GameObject projectile;
public float bulletSpeed;
void Update () {
if (Input.touchCount > 0) {
if (Input.GetTouch (0).phase == TouchPhase.Began) {
Vector2 touchPos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
Vector2 dir = touchPos - (new Vector2(transform.position.x, transform.position.y));
dir.Normalize ();
GameObject bullet = Instantiate (projectile, transform.position, Quaternion.identity)as GameObject;
bullet.GetComponent<Rigidbody2D> ().velocity = dir * bulletSpeed;
}
}
}
}
Thank you so much u are a life saver, just what i was looking for, the secret was the formula of subtracting the Vector2(transform.position.x,transform.position.y) from the touch position!
Answer by edcx · Nov 23, 2016 at 09:49 AM
There is a function called ScreenPointToRay. This function creates a ray from camera to point on the screen.
Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(r.origin, r.direction * 10, Color.cyan);
You need a direction vector and you can get that from above code. Replace mouse position with your touch position. You need to move your projectile along the ray's direction. And you can instantiate your bullet like this:
GameObject bullet = Instantiate (projectile, transform.position, Quaternion.LookRotation(r.direction))as GameObject;
I hope this helps.
I will definitely be using the ray for future parts of this build, so thank you for the tips. I ended up patching some things and getting it to work. The script is below. Thank you for the quick response.
Answer by Mister-Mortal · Nov 23, 2016 at 09:23 AM
First of all you are using Camera.main.ScreenToWorldPoint() wrong. You pass it touch position, but touch position only has x and y values, z is zero, but ScreenToWorldPoint won't work without z coordinate.
If your game is 2D it's pretty easy to calculate correct z, here's the example:
Camera.main.ScreenToWorldPoint(new Vector3(
Input.GetTouch(0).position.x,
Input.GetTouch(0).position.y,
-Camera.main.transform.position.z + zPositionOfTheObjectYouWillRotate));
The second thing that could cause problems is LookAt() method, it has a second parameter which tells which way is the world up direction.
bullet.transform.LookAt(touchPosition, Vector3.forward);
I think this should do the trick, but if it doesn't play around with the second parameter.
Thank you for your help! I wasn't thinking enough about 2d and once i read your comment it hit me (with the help of a little different research). Full script it below if your curious.
Your answer
![](https://koobas.hobune.stream/wayback/20220612101232im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Touch.position is incorrectly offset 0 Answers
To use touch & raycasting to *drag and drop* a game object... 1 Answer
Space Shooter Mobile Touch Controls Issue 1 Answer
Need some Help with Swipes! 0 Answers
touch camera jumping to touch position 0 Answers