Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by raulGLD · Oct 11, 2018 at 01:35 PM · android2d2d gamegamehealth

Drag and release

I need help with (what I believe to be simple but I don't know) something (for android): I have a 2D ball, that is on a simple square table, I want to drag from that ball and release and upon release the ball goes in that direction or in opposite direction if you drag "behind" the ball. I don't need aim, or arrows or anything. Just put your finger on the ball then drag and release. I searched for 2 days on forums and youtube and everywhere but I couldn't find what I need. I found something but it's really outdated and does not help me. Thank you for your help! And I am sorry for taking your time if there is already something answered that I didn't find!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by raulGLD · Oct 12, 2018 at 11:19 AM

EDIT: THE RIGHT CODE IS NOW POSTED I found it, hope this helps someone else too!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Swipe : MonoBehaviour {
 
     Rigidbody2D rbody;
     Vector2 startpos;
     Vector2 endpos;
     float power = 5f; // power of shot
     
 
     // Use this for initialization
     void Start () {
 
         rbody = GetComponent<Rigidbody2D>();
 
     }
     
     // Update is called once per frame
     void Update () {
 
         
 
             if (Input.GetMouseButtonUp(0))
             {
                 endpos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                 LaunchBall();
                 
             }
         }
 
     }
 
     void OnMouseDown()
     {
         startpos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     }
 
     void LaunchBall()
     {
         Vector2 direction = (startpos - endpos); // swap subtraction to switch direction of launch
         rbody.AddForce(direction * power, ForceMode2D.Impulse);
     
 
 }
 



Comment
Add comment · 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 raulGLD · Oct 13, 2018 at 08:05 AM 0
Share

Still, I need help with something. I have an object that moves when I swipe on the screen but it also rotates and if I swipe again the same way the object might go another way due to the fact that it rotated. I don't want to freeze it's rotation I just want to make it work regardless it's rotation. Could you please help me? Thank you!

avatar image Bunny83 raulGLD · Oct 13, 2018 at 08:58 AM 0
Share

AddForce is immune to object rotations. So the rotation of an object does not affect the result of the added velocity. If you want to cancel all current momentum and just move in the direction you swipe, don't use AddForce but ins$$anonymous$$d set the velocity directly.


Note that your code has a general flaw and may only work for a single object. Currently you only record the start position when you have a mouse down on the object. However the mouse up event will always be detected regardless of where you clicked down initially. So if you release your mouse all objects with this script will react to the mouse up but with their old start position.


If you want to accelerate single objects you should use a boolean to remember that you clicked on this object and only apply a force on $$anonymous$$ouseUp when that boolean is true. Don't forget to reset that bool once you applied the force.


If all objects with this script should be affected you shouldn't use On$$anonymous$$ouseDown since that is only detected for the object the mouse is over. As far as i remember your original code actually used Get$$anonymous$$ouseButtonDown inside Update.

avatar image raulGLD Bunny83 · Oct 13, 2018 at 09:22 AM 0
Share

Yes you are right about that start position. I tried but can't figure out how to make it reset the ball position so I can swipe wherever I want not just on the ball start position. Can you please help me? Thank you!

avatar image
0

Answer by Adygha · Sep 11, 2020 at 11:36 AM

How can i use it with touch controls not mouse input ?

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

308 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 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 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 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 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 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 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 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 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 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 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 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 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

2D Android game - bad performance after launch 1 Answer

2d Lighting - Weird light triangles 0 Answers

Touching moving sprites 0 Answers

How to choose a size/resolution for the sprites used as game assets in Unity2D ? 2 Answers

Things to know about 2D games! 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