Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by splitter20 · Nov 23, 2016 at 07:39 AM · mobiletouchshooting

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;

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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; 
             }
         }
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rocamalo · Jul 17, 2021 at 02:10 AM 0
Share

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!

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image splitter20 · Nov 23, 2016 at 11:05 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image splitter20 · Nov 23, 2016 at 11:04 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

88 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges