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 m_foxy · Feb 09, 2015 at 10:33 PM · raycastunity 2dtouchswipe

How to flick a game object using touch and raycasters ?

I've been searching for a solution for my problem for a while on the web, but i didn't find anything that work properly

What i want to do The game is in 2D mode. I have a bouncing ball and I want to let the user flick it with his finger (using a swipe gesture or whatever). The ball shouldn't start moving unless the player flick touch the ball. I don't want the ball to move if the player flicks anywhere on the screen, only if the flick touch the ball

What i've done so far :

I've tried to write a script which would detect the touch starting position and the touch end position, and then cast a ray from that point and check if it hit the ball to make sure that the flick touch the ball. Then the script calculate the direction vector by substrating the start position from the end position, and then give the ball a velocity vector which is the direction vector

The code inside my update methode :

 if (Input.touchCount > 0) {
                         Touch t = Input.touches [0];
                         if (t.phase == TouchPhase.Began) {
                                 start = Camera.main.ScreenToWorldPoint (new Vector3 (t.position.x, t.position.y, 0));
                         }
             
                         if (t.phase == TouchPhase.Ended || t.phase == TouchPhase.Canceled) {
                 end = Camera.main.ScreenToWorldPoint(new Vector3(t.position.x,t.position.y,0));
                 direction = end-start ;
                 Ray ray = Camera.main.ScreenPointToRay(new Vector3(end.x,end.y,0.00f));
                 RaycastHit hit;
                 if ( Physics.Raycast(ray, out hit,1000.00f))
                 {
                     print("touched"+hit.transform.gameObject.name);
                     rigidbody2D.velocity = direction;
                 }
             }
                 }

What's the problem ?

The problem is that the code doesn't work.

When i launch the game in the Unity Remote 4 app in my android device and try to flick the ball it doesn't move, the inspector shows that the the scripte is calculating the start & end positions and the direction vector, but the ball doesn't move, it stays in the same position even when the flick touch the ball. I am thinking that the raycast is not hitting the ball, but why ? How should i fix it ? is there a better way to do it ?

Since the ball have a 2D circle collider how to make it detect collision with the player swipe ?

Thank's !

Comment
Add comment · Show 4
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 smoggach · Feb 09, 2015 at 11:02 PM 0
Share

You'll need to cast your ray with Physics2D in order to hit 2D colliders.

avatar image m_foxy · Feb 10, 2015 at 02:06 PM 0
Share

I did it's not working !

avatar image smoggach · Feb 10, 2015 at 02:35 PM 0
Share

You'll also need a RaycastHit2D.

avatar image smoggach · Feb 10, 2015 at 02:38 PM 0
Share

Just use TouchScript. It has a flick gesture that already does all this. https://www.assetstore.unity3d.com/en/#!/content/7394

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Moving a 3D object with 2D swipes 2 Answers

Move gameObject with swipe in 3d space ? 1 Answer

Touch interface not working 1 Answer

Button Focus Prevents Touch Raycast (Android C#) 1 Answer

Paddle script for Pong-based video game for Android 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