• 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 drorriov · Jun 21, 2014 at 03:31 PM · javascript

Move Object by dragging (mobile) makes the balls fall from the cup

I have this cup that contains balls inside. All 2D I am using Rigidbody2d and Collider2d.

When running in unity and moving the cup (with arrow keys) the balls stay inside the cup. I also added drag movement for Android touch to move the cup.

The problem is that when moving the cup too fast (by draging) the balls fall from the cup collider (using Polygon colider 2d).

Code for movement is:

  public float speed = 0.1F;
     void Update() {
         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
             Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
             transform.Translate(-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);
         }
     }

I tried to play with the speed parameter but it wont really help. if the cup movement is too slow it is not very useful for me. I believe it is related the velocity/force of the ball or cup which makes the cup collider miss...

Edit: I tried to increase the size of the colliders using 2d box colliders instead of Polygon 2d collider and still it doesn't work.

Any help on this would be appreciated greatly...

Comment

People who like this

0 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 Josh707 · Jun 23, 2014 at 07:41 PM 0
Share

I would also try using Rigidbody2D.MovePosition rather than Translate.

avatar image drorriov · Jun 24, 2014 at 06:41 AM 0
Share

I tried the following: private var speed : Vector3 = Vector3 (3, 0, 0); function FixedUpdate () { if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) { rigidbody.MovePosition(rigidbody.position + speed * Time.deltaTime); } }

But the rigidbody2d does move at all on touch..

avatar image Josh707 · Jun 24, 2014 at 08:58 AM 0
Share

Shouldn't you be using rigidbody2D instead of rigidbody? I thought there would be a warning about no rigidbody being attached but there isn't. It was added just in version 4.5 so you might need to update to use it.

avatar image drorriov · Jun 24, 2014 at 01:16 PM 0
Share

I did try with rigidbody2D but the cup did not move with touch.. my mistake when copy/pasting here.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by ABerlemont · Jun 24, 2014 at 08:58 AM

I worked on a similar project. Never managed to get that to work ... Even with slow physic/rigidbody/MovePosition2D()

Best option for now is to move the cup by using physic forces, not MovePosition()/transform.position

 // Apply a force that attempts to reach our target velocity
 Vector3 velocityChange = ...;
 controller.AddForce(velocityChange, ForceMode.VelocityChange);

Hoped 4.5 would change something but it didn't.

Comment

People who like this

0 Show 3 · 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 drorriov · Jun 24, 2014 at 01:26 PM 0
Share

Do i add this to the fixedupdate() inside if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)?

avatar image ABerlemont · Jun 27, 2014 at 12:51 PM 0
Share

I think that you need to get the difference between the origin position and destination (based on touch behavior) and apply that diff to velocity.

avatar image drorriov · Jun 30, 2014 at 03:18 PM 0
Share

Thank you for the reply. Can you provide an example for this?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to drag an object with touch using Rigidbody2D.MovePosition 1 Answer

touch.phase == TouchPhase.Canceled not worked 1 Answer

Rotating a spirit, right to left, how to limit rotation angel? 1 Answer

Texture2d.Readpixel slow down performance while stand-alone exe is minimize 0 Answers

How to handle 5 touches at a time? 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