• 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
Question by Adecker · Feb 29, 2012 at 12:00 AM · androidiosmobiletouchshoot

Raycast from player to touch point?

I am creating a 2D side-scroller for my first game and I have quite a bit done; however, one thing has been giving me trouble for around a week now. I want to shoot where the user taps, so I figure I need to raycast to the point where the user taps and shoot a projectile there and then destroy it when it collides with an object? I have searched and cant find anything related to this, I found examples for raycasting from the camera to a touch but not from the actual character. Any ideas?

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by syclamoth · Feb 29, 2012 at 12:16 AM

Well, in this case you need two things (or possibly 3).

First, you need to know what plane your character is moving on. Usually this would be as simple as creating one with:

 Plane dudePlane = new Plane(player.position, Camera.main.forward);

Then, to get the touch position, you can use

 float point;
 Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition)
 dudePlane.Raycast(camRay, out point);

 Vector3 position = camRay.GetPoint(point);

Then, finally, get the actual ray by using

 Ray shotRay = new Ray(player.position, (position - player.position));

Now, you can use this ray however you like- for shooting a projectile, for calculating a hitscan, anything.

Comment

People who like this

0 Show 5 · 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 syclamoth · Feb 29, 2012 at 12:17 AM 0
Share

Yeah, actually 3.

avatar image Adecker · Feb 29, 2012 at 02:27 AM 0
Share

is that C#, boo, or js? I cant tell lol

avatar image syclamoth · Mar 01, 2012 at 06:38 AM 0
Share

This is pretty obviously C#.

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

How can i use that ray with a shoot function that uses a rigidBody and addForce?

avatar image rohit_garg · Apr 17, 2013 at 01:38 PM 0
Share

@syclamoth can you please help me out in the case where i want to move my player to touch position on Android device gradually but not instantly.

avatar image

Answer by ChrisMasterton · Feb 29, 2012 at 12:29 AM

First convert the touch position into world coordinates. Then you can raycast from the players position to the target position in world space.

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

getPressure for touch pressure on Android or iOS 3 Answers

Receiving UI/touch events while mobile soft keyboard is open 0 Answers

Avoid touch input when pressing the "Pause" button 1 Answer

Mobile - lingering touch even after finger lifts 0 Answers

Different animations on mobile 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