i can't shoot to touch position

hi guys, i’m a beginner to unity technology and this is my first question in this forum.

i have a little problem, the problem is i can’t shoot my prefab to the touch position, i googled around a lot…but nothing useful even that with my current code i can shoot to the position of touch but i can’t shoot to left or right its just stuck its only goes forward

this is my code i attached to my main camera in the scene

using UnityEngine;

public class TouchManager : MonoBehaviour
{

    public Rigidbody projectFile;
    public Transform firePosStart; //empty game object to get the start posX,Y,Z


    void Update()
    {


        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        {

            Touch myTouch = Input.GetTouch(0);


            Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
            Rigidbody clone;

            projectFile.transform.LookAt(new Vector3(myTouch.position.x, myTouch.position.y));

            clone = Instantiate(projectFile, firePosStart.transform.position, firePosStart.transform.rotation) as Rigidbody;
           
            clone.AddForce(projectFile.transform.forward * 2000);
               
           
        }
       
    }

}

and this a the result as u can see the fireball is moving forward no matter where i click what’re is that even if i click left or right the fireball keep moving forward and this is not what i want, i want the fireball to go to the touch position

please not that i don’t care to my script u can change it as u like :slight_smile: feel free to make the fireball shoot to thetouch position as u like :slight_smile:
please if anyone have any thoughts please share it with me cause this is start become annoying and thanks anyway :slight_smile:

close, on line 20 you start the necessary code to change the screenspace touch position into a world space position… but you don’t complete it.

In line 23 you should be using a worldspace position.

You need to make use of the ray you have, have a look at 09:00 onwards

try

clone.AddForce(clone.transform.forward * 2000);

than you are creating a new Vector3 with just 2 values

and you should not rotate your prefab, instead rotate the already Instantiated clone

also as you instantiate your clone, you are using the rotation of your firePosStart which doesn’t change
what does the ray do ? not using it ?

if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) {

   Vector3 myTouch = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);

   firePosStart.transform.LookAt(myTouch);

   Rigidbody clone = Instantiate(projectFile, firePosStart.transform.position, firePosStart.transform.rotation) as Rigidbody;
              
   clone.AddForce(clone.transform.forward * 2000);
}
1 Like

thanks for your response but still i can’t figure it out ??
can u give me more detail ?

Thank you i will try this :slight_smile:

1 Like

Hope it will work, a thing could be the Z Value of the Touch.position