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 daclu_10 · Jun 09, 2014 at 03:24 AM · raycasttouchtouchscreen

Select and drag a gameObject with Touch

Hi, i'm developing my first game, and its a 2d game.

I need to select a square an drag it to collide another one, with a touch.

i just cant get it moving, i cant post my code cause i've tried so many things i would not know what to post.

also i think this might be so basic, but i've tried for hours and hours and cant gat that simple result.

detect touch, as i detected the touch then the gameobject on that position gets selected, and then drag it.

i've tried mainly with raycasting 2d, but have trouble every time, i'm so stuck at this point. i think i understand the principles of raycasting and raycast hit, but cant make it work.

PLEASE, I USE JAVASCRIPT!!!!! i've found more info in c# but it gets hardier to me to try things up.

Please help!

Comment
Add comment · Show 3
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 daclu_10 · Jun 09, 2014 at 02:42 PM 0
Share

well for example i've tried this:

function Update () {

 for (var touch : Touch in Input.touches){
 
     var ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
 
     var hit : RaycastHit2D;
     
       if (Physics2D.Raycast (ray, hit)) {
        
        //here i dont know how to select the gameObject that was hit 
        //so i can move it as i move the finger,it is maybe the collider?  

        collider2D.gameObject.transform.position = touch.position;
 
     }

}


But in this part : if (Physics2D.Raycast (ray, hit))

I always get this error:

BCE0023: No appropriate version of 'UnityEngine.Physics2D.Raycast' for the argument list '(UnityEngine.Ray, UnityEngine.RaycastHit2D)' was found.

And dont know how to fix that.

Thanks a lot for your help!

avatar image Landern · Jun 09, 2014 at 02:48 PM 0
Share

Physics2D.Raycast doesn't have an overload like that from what the documentation says:

http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html

The 3D version of the Physics class does however.

avatar image daclu_10 · Jun 09, 2014 at 03:36 PM 0
Share

but can i use 3d classes in a 2d game?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Aaeronn09 · Jun 09, 2014 at 08:16 AM

I think you can try delta position with touch..

Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;

This will calculate the touch by frames.. Try research on this. You havent posted the code so it wont be easy to say whats the problem.. If you can try posting the code..

Comment
Add comment · 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
0

Answer by Andres-Fernandez · Jun 09, 2014 at 06:55 AM

Maybe there's an easier way of doing it, but here's how I would do it:

First, set your sprite to a layer like "draggable" or similar. Second, attach a collider to it. Then use a raycast from the camera. Something like ViewportPointToRay, using the touch.position as the input, and check if that collides with the layer of your sprite. If it does, then you just need to set its position (every frame) based on the touch.position until you get the touch.phase == TouchPhase.Ended.

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

Raycasting UI elements - Without using Event Triggers 0 Answers

Raycast not working on touchscreen (Android tablet/phone) 2 Answers

Touch dont work 1 Answer

Make an Object follow your touch 0 Answers

Touch interface not working 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