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 07:57 PM · raycastcollidertouch

select and drag an object with touch

Hi, im working on a 2d game.

i've finding that its very complicated to move an object taping it with your finger and then draging it to somewhere else, i looked up on a bunch of sites an a lot of different codes, and i still dont gat that result. I think i'm missing something cause in my opinion that could be one of the main things you want to find when developing a game for a smartphone, i just dont get why its so hard to find how to do that. just recongnize the touch, select an object that its at the spot you've just touched, and then be able to drag it anywhere you want, as the object its copying the touch deltaPosition

this is the last code i've used:

pragma strict

function Update () {

for (var touch : Touch in Input.touches){

 var ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).deltaPosition);

 var hit : RaycastHit2D;

   if ((hit.collider != null)) {

   hit.collider.gameObject.transform.position = Touch.deltaPosition;

 }
  

} }


in this case the error is:

NullReferenceException: Object reference not set to an instance of an object

i get this result when i touch the screen.

and i am also getting this:

touch.js(13,52): BCE0022: Cannot convert 'UnityEngine.Ray' to 'UnityEngine.Vector3'.

PLEASE HELP!!!

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

Answer by supernat · Jun 09, 2014 at 08:18 PM

The easiest approach is to not use raycasts. Attach a collider to the object you want to drag (if not already attached). Add a script to that object with OnMouseDown(), OnMouseUp, and Update() methods. In OnMouseDown, set a flag indicating dragging is active. In the OnMouseUp, reset that flag. Finger touches will be sent through the same Mouse events. Then in Update(), if the drag flag is set, position the game object's transform to the current mouse position (you'll have to figure out your own Z value if it's in 3D).

 // Something like this (it's untested, there may be some extra work involved, but the idea is generally the following and
 // it should work for an orthographic 2D camera if Z is your fwd/back axis, 3D gets more complicated, 
 // because the Z value is not necessarily into/out of screen
 void Update() {
     if (dragging) {
         Vector3 posn = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         posn.z = 0; // or whatever depth you want the game object to remain at, you could save it off ahead of time and reset it here
         transform.position = posn;
     }
 }

Obviously, if you're doing this in 3D, it's much more complicated, because you have to define a depth to place the object at. If doing that, the way I would approach it is to calculate the distance to the object in the OnMouseDown event, then when you get a returned position from ScreenToWorldPoint, you normalize that value and extend it by the distance you saved off, so the object will remain at the same distance from the player no matter where they drag it.

Comment
Add comment · Show 2 · 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 daclu_10 · Jun 10, 2014 at 01:46 AM 0
Share

sorry, when you say a "flag", do you mean a boolean variable?

avatar image daclu_10 · Jun 10, 2014 at 02:48 PM 0
Share

Thanks a lot, it really helps me out!

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

22 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

Related Questions

How to hit two object with one raycast? 2 Answers

Multiple hit detection with Raycasting? 2 Answers

Touch script using raycast on a collider.. :( 0 Answers

Dragging an object in iOS (UnityScript) 3 Answers

Applying damage with Flamethrower-like weapon 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