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 xikky · Mar 11, 2013 at 01:38 PM · rigidbodypositiontouchdragging

Need a helping Hand: Dragging a rigidbody on touch move.

I having trouble implementing a function to drag a rigidbody platform when the user moves a finger on the screen. I want to move the rigidbody only on the X axis and I want the rigidbody platform object to act naturally under physics all the time.

I did try many ways to make this work and I actually managed to do it, but the dragging of the rigidbody is still not so comfortable to use and collisions are not working at their best either (and I'm guessing the latter is happening due to high speeds). I need the dragging to be as easy as possible as this will be used to control a platform to prevent a ball from falling.

This is my current work, it makes the object move quite well but I really wish to improve it.

  • I'm applying a boundary limit in which the object is allowed to move on the X asix. The object is allowed to be moved between 400 and 880 world points.

  • My idea to make this work is to calculate the difference between the touch position and the rigidbody's position, and increase the rigidbody's velocity by the difference found.

  • Check out the screen shots below to understand the setup.

    function FixedInputs() {

       if (Input.touchCount > 0 && Input.touchCount < 3) {
             
             for (var touch:Touch in Input.touches){
                 
                 if (touch.phase == TouchPhase.Moved && playerSlider.HitTest(touch.position)){
                     
                     touchXPos = touch.position.x - ((Screen.width / 10) * 2);
                     playerPos = camera.main.WorldToScreenPoint(playerRigidbody.position);
                     playerXPos = playerPos.x;
                     
                     if (playerRigidbody.position.x > 400 && ((touchXPos - playerXPos) * 6.5)/touchDivider < 0 && ((touchXPos - playerXPos) * 6.5)/touchDivider  > -10){
                         playerRigidbody.velocity = (new Vector3(1, 0, 0) * 1000 * ((touchXPos - playerXPos) * 6.5)/touchDivider/8 * Time.fixedDeltaTime);
                     }
                     else if (playerRigidbody.position.x < 880 && ((touchXPos - playerXPos) * 6.5)/touchDivider > 0 && ((touchXPos - playerXPos) * 6.5)/touchDivider  < 10){
                         playerRigidbody.velocity = (new Vector3(1, 0, 0) * 1000 * ((touchXPos - playerXPos) * 6.5)/touchDivider/8 * Time.fixedDeltaTime);
                     }
                     else if (playerRigidbody.position.x > 400 && ((touchXPos - playerXPos) * 6.5)/touchDivider < 0 && ((touchXPos - playerXPos) * 6.5)/touchDivider  > -30){
                         playerRigidbody.velocity = (new Vector3(1, 0, 0) * 1000 * ((touchXPos - playerXPos) * 6.5)/touchDivider/2 * Time.fixedDeltaTime);
                     }
                     else if (playerRigidbody.position.x < 880 && ((touchXPos - playerXPos) * 6.5)/touchDivider > 0 && ((touchXPos - playerXPos) * 6.5)/touchDivider  < 30){
                         playerRigidbody.velocity = (new Vector3(1, 0, 0) * 1000 * ((touchXPos - playerXPos) * 6.5)/touchDivider/2 * Time.fixedDeltaTime);
                     }
                     else if (playerRigidbody.position.x > 400 && ((touchXPos - playerXPos) * 6.5)/touchDivider  <= -30){
                         
                         if ((touchXPos - playerXPos) < -40)
                         playerRigidbody.velocity = (new Vector3(1, 0, 0) * 1000 * (-70 * 6.5)/2 * Time.fixedDeltaTime);
                         else 
                         playerRigidbody.velocity = (new Vector3(1, 0, 0) * 1000 * ((touchXPos - playerXPos) * 6.5)/2 * Time.fixedDeltaTime);
                     }
                     else if (playerRigidbody.position.x < 880 && ((touchXPos - playerXPos) * 6.5)/touchDivider  >= 30){
                         
                         if ((touchXPos - playerXPos) > 40)
                         playerRigidbody.velocity = (new Vector3(1, 0, 0) * 1000 * (70 * 6.5)/2 * Time.fixedDeltaTime);
                         else 
                         playerRigidbody.velocity = (new Vector3(1, 0, 0) * 1000 * ((touchXPos - playerXPos) * 6.5)/2 * Time.fixedDeltaTime);
                     }
                     else{
                         playerRigidbody.velocity = (new Vector3(0, 0 , 0) * Time.fixedDeltaTime);
                     }
                 }
                 else playerRigidbody.velocity = (new Vector3(0, 0, 0) * Time.fixedDeltaTime);
             }
         }
         else playerRigidbody.velocity = (new Vector3(0, 0, 0) * Time.fixedDeltaTime);
    

}

To be honest I'm not 100% sure of what I'm doing here. The result of this code is a couple of days work of trying to figure out a logic to make the dragging happen.

I'm still not pleased with the work I have. Is there anybody who can provide me with a better way to do the dragging.

This is what I am trying to do:

  • The red box is the space in which the rigidbody is allowed to move (X axis, does not move on Y axis)

  • The green box is the space (GUITexture) on which the user will drag his finger to correspond to the movement of the platform. When the user drags his finger to the left or right side of the green box, the platform should move to the left or right side of the red box, respectively.

alt text

drag touch.jpg (65.1 kB)
Comment
Add comment · Show 5
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 xikky · Mar 12, 2013 at 02:43 PM 0
Share

I hope this question is clear enough.

avatar image mezkal · May 05, 2013 at 05:26 PM 0
Share

Hey mate, did you managed to do this? :) I'm struggling in your footsteps here...

avatar image ExTheSea · May 05, 2013 at 05:27 PM 0
Share

$$anonymous$$ezkal i converted your answer to a comment because answers should really just be used to answer the main question.

  • Read this page : http://answers.unity3d.com/page/newuser.html

  • Please watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

avatar image ExTheSea · May 05, 2013 at 05:29 PM 0
Share

Btw. i don't have the time to read through you're entire question but maybe this helps: http://www.youtube.com/watch?v=QjUhQ4z6pF0&feature=youtu.be

avatar image xikky · May 07, 2013 at 07:18 AM 0
Share

@mezkal no I couldn't find a way to make this work smoothly, so I changed my method of input touch.

@ExTheSea thanks for the video. Will check it out later when I have time.

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

12 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

Related Questions

Moving selected unit to location in space rts based on screen touch 3 Answers

Is there a way to set velocity while setting the position? 1 Answer

Why does "draggable" MonoBehavior cause the object to be moved in the opposite direction? 1 Answer

Get latest touch position? 2 Answers

check user touching a particular position or not 2 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