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 oziriz · May 10, 2012 at 02:35 PM · touchshootraytap

Shooting with touch

Hi! I am developing a 3d shooter and i was trying to shoot my enemy using rigidbody and then addForce. It all works great when i use the mouse but i wanted to use small taps to shoot bullets. I am using the following code to catch the touch input:

My goal is to shoot the area that i am touching (without moving the cam)

// touch,tap code

float point;

 foreach(Touch touch in Input.touches){
         
 player = new Vector3(GameObject.Find("Player").transform.position.x,GameObject.Find("Player").transform.position.y,GameObject.Find("Player").transform.position.z);
     
 Plane playerPlane = new Plane(player,Camera.main.transform.forward);//VER ESTA LINHA
         
 Ray camRay = Camera.main.ScreenPointToRay(touch.position);
 playerPlane.Raycast(camRay,out point);
         
 Vector3 position = camRay.GetPoint(point);
         
 Ray shotRay = new Ray(GameObject.Find("Player").transform.position, (position - GameObject.Find("Player").transform.position));
         
         Instantiate();//function that does the shooting
         canShoot = false;
     }

//shooting code

Transform crate = Instantiate(cratePrefab, transform.position, Quaternion.identity) as Transform;

     Physics.IgnoreCollision(crate.collider, collider);

crate.rigidbody.AddForce(transform.forward * 5000);

How can i use the "ray" with the function i have to shoot bullets?

Comment
Add comment · Show 2
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 syclamoth · May 10, 2012 at 02:37 PM 0
Share

Just in general, you should cache the player gameObject, ins$$anonymous$$d of using GameObject.Find(name). Totally unrelated to the question, but it really bugs me every time I see someone using GameObject.Find for anything.

avatar image oziriz · May 10, 2012 at 02:54 PM 0
Share

I started unity very recently...tanks anyway ;)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Swift_On_Fire · May 10, 2012 at 03:08 PM

I agree with syclamoth about caching the player object. That aside...

I don't see why you are using a Plane here. This looks more complicated than it has to be. Is this a 3rd person view or a 1st person view?

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 oziriz · May 10, 2012 at 03:13 PM 0
Share

1st person! I followed a lead by another user -> http://unity3d.qatohost.com/questions/222121/raycast-from-player-to-touch-point.html

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

6 People are following this question.

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

Related Questions

Tap Where To Shoot 2D 1 Answer

Raycast hitting below mouse position :( 0 Answers

Ray from a cube to a touch point to help aiming 0 Answers

Taps are not detected on mobile 1 Answer

TouchPhase.Ended does not work if ray is not hitting object. 1 Answer


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