Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

i can't shoot to touch position

Discussion in 'Scripting' started by Ayoub Dev, Jan 26, 2016.

  1. Ayoub Dev

    Ayoub Dev

    Joined:
    Jan 25, 2016
    Posts:
    3
    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

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TouchManager : MonoBehaviour
    4. {
    5.  
    6.     public Rigidbody projectFile;
    7.     public Transform firePosStart; //empty game object to get the start posX,Y,Z
    8.  
    9.  
    10.     void Update()
    11.     {
    12.  
    13.  
    14.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    15.         {
    16.  
    17.             Touch myTouch = Input.GetTouch(0);
    18.  
    19.  
    20.             Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    21.             Rigidbody clone;
    22.  
    23.             projectFile.transform.LookAt(new Vector3(myTouch.position.x, myTouch.position.y));
    24.  
    25.             clone = Instantiate(projectFile, firePosStart.transform.position, firePosStart.transform.rotation) as Rigidbody;
    26.            
    27.             clone.AddForce(projectFile.transform.forward * 2000);
    28.                
    29.            
    30.         }
    31.        
    32.     }
    33.  
    34. }
    35.  
    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

    Capture.PNG

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

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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
     
  3. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    try

    Code (CSharp):
    1. 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 ?
     
    Last edited: Jan 26, 2016
  4. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    Code (CSharp):
    1. if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) {
    2.  
    3.    Vector3 myTouch = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
    4.  
    5.    firePosStart.transform.LookAt(myTouch);
    6.  
    7.    Rigidbody clone = Instantiate(projectFile, firePosStart.transform.position, firePosStart.transform.rotation) as Rigidbody;
    8.              
    9.    clone.AddForce(clone.transform.forward * 2000);
    10. }
     
    Ayoub Dev likes this.
  5. Ayoub Dev

    Ayoub Dev

    Joined:
    Jan 25, 2016
    Posts:
    3
    thanks for your response but still i can't figure it out ??
    can u give me more detail ?
     
  6. Ayoub Dev

    Ayoub Dev

    Joined:
    Jan 25, 2016
    Posts:
    3
    Thank you i will try this :)
     
    martinmr likes this.
  7. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    Hope it will work, a thing could be the Z Value of the Touch.position