Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Ekta-Mehta-D · Sep 30, 2013 at 11:48 AM · 2dtouchbulletshoot

Shoot Bullet At Touch Position : 2D

Hello everyone,

I have one object shooting in x-y plane..,x means horizontal and y means vertical direction.. My bullet is capsule game object with rigidbody component.

Now Whenever i touch on the screen , i want my bullet to translate towards that position.

For touch i have written code as below:

 for (var i = 0; i < Input.touchCount; ++i) {
             ray = Camera.mainCamera.ScreenPointToRay(Input.GetTouch(i).position); 
             switch(Input.GetTouch(0).phase)
             {
                case TouchPhase.Stationary:
                if (Physics.Raycast(ray,hit))
                {        
                     
                                 bulletObject = Instantiate(bulletPrefab , bulletShootPoint.transform.position , bulletPrefab.transform.rotation);
 //                                hitPoint = hit.point;
 //                                var pos = Input.GetTouch(i).position;
                                 hitPoint = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position);
                                 
                                    
                }
                
                case TouchPhase.Began:
                break;
                 
                  case TouchPhase.Moved:
                break;
                
                case TouchPhase.Ended:
                break;
                     
             }    
         }

Which code should i write to shoot bullet at exact touch position? Please help me to solve this action. Please guide me.

Thanks in advance for helping me and supporting me..

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

1 Reply

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

Answer by robertbu · Sep 30, 2013 at 04:30 PM

I have no idea about your aiming mechanic. But I can address the specific of how to instantiate an object at the touch position. With a 2D game, the camera will usually be some fixed distance from the 2D surface. For example the camera may be at -10 and the surface at 0 on the 'z' axis for a distance of 10 units. Whatever your setup, you need the distance from the camera to the playing surface. To convert a touch into a world space position you can use Camera.ScreenToWorldPoint(). The 'z' component is the positive distance in front of the camera. So using your code above and a 10 unit distance, you could may be:

 case TouchPhase.Stationary:
     var pos = Input.GetTouch(i);
     pos.z = 10;
     pos = Camera.main.ScreenToWorldPoint(pos);
     bulletObject = Instantiate(bulletPrefab, pos, Quaternion.identity);


Comment
Add comment · Show 3 · 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 Ekta-Mehta-D · Oct 01, 2013 at 06:16 AM 0
Share

sir i have edited my question. I want my bullet to translate at exact touch position.

avatar image robertbu · Oct 01, 2013 at 12:14 PM 0
Share

The easiest way to get your bullet to translate towards the hit point is to have the bullet look at the hit point and then in Update() have the bullet translate forward:

Insert at line 13 above:

 bulletObject.transform.LookAt(hitPoint);

Then on the script on the bullet:

 transform.Translate(Vector3.forward * speed);

Alternately if you are going to be using the physics engine, you can add force to the bullet ins$$anonymous$$d. For physics, add a Rigidbody to the bullet, probably turn off gravity, and then insert the following two lines at line 13 above:

 bulletObject.transform.LookAt(hitPoint);
 bulletObject.rigidbody.AddForce(bulletObject.transform.forward * 1000);
avatar image Ekta-Mehta-D · Oct 01, 2013 at 12:34 PM 0
Share

ok thanks sir. I solved my problem..

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

14 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

Related Questions

Projectile move towards mouse cursor 3 Answers

I can't shoot up or down. HELP! 1 Answer

Fire Bullet To Mouse Position Problem 2 Answers

RayCast Problem : 2d 1 Answer

Problem creating a 2D scroller shooting game 2 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