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
2
Question by newbieguy · Jan 16, 2014 at 06:06 PM · 2dphysics2d-physicsdragthrow

How to drag and throw 2D objects?(SOLVED)

I have a script for dragging objects with my mouse while click1 is pressed but I don't know how to throw them on release.I want the objects to be thrown at the same speed I move the mouse while I'm dragging it. I want to make a character who has special powers kind of like The Force(star wars) but in 2D.

This is what I use for dragging:

        void OnMouseDrag()
         
     {
         
         Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         
         point.z = gameObject.transform.position.z;
         
         gameObject.transform.position = point;
         
         Screen.showCursor = false;
 
         
     }
     
     
     
     void OnMouseUp()
         
     {
         
         Screen.showCursor = true;
         
     }
 
 !SOLVED!

Look below for answer!

Comment
Add comment · Show 1
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 Vardan Meliksetyan · Jan 16, 2014 at 06:15 PM 0
Share

http://answers.unity3d.com/questions/617582/cue-move-as-mouse-drag-problem.html

Look this code,

5 Replies

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

Answer by newbieguy · Jan 16, 2014 at 10:15 PM

I found a video that shows how I would like the dragging to be.

Youtube Video

Solved!

 using UnityEngine;
 using System.Collections;
 
 public class NewBehaviourScript : MonoBehaviour {
     
     public SpringJoint2D spring;
 
      
     void Awake()
     {
 
     spring = this.gameObject.GetComponent<SpringJoint2D>(); //"spring" is the SpringJoint2D component that I added to my object
     
     spring.connectedAnchor = gameObject.transform.position;//i want the anchor position to start at the object's position
     
     }
 
 
     void OnMouseDown()
     {
     
     spring.enabled = true;//I'm reactivating the SpringJoint2D component each time I'm clicking on my object becouse I'm disabling it after I'm releasing the mouse click so it will fly in the direction i was moving my mouse
 
     }
 
 
     void OnMouseDrag()        
     {
     
             if (spring.enabled = true) 
             {
 
                 Vector2 cursorPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);//getting cursor position
                 
                 spring.connectedAnchor = cursorPosition;//the anchor get's cursor's position
                  
 
             }
     }
 
     
     void OnMouseUp()        
     {
     
     spring.enabled = false;//disabling the spring component
                 
     }
 
 }

Values for SpringJoint2D Component: Distance: 0.005(minimum) Damping Ratio: 0 Frequency:1

I also changed the Ridibody2D Linear Drag value to 1 so the spring oscilation would stop.

I have't played too much with the values but the ones above are ok for me.

How I got to this solution?: I posted a video on youtube above showing Dragging Physics handled in Corona SDK, so i searched for the .lua file and saw they were using a Joint to do it.

Comment
Add comment · Show 5 · 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 Vardan Meliksetyan · Jan 16, 2014 at 10:27 PM 0
Share

Wait I am developing game where I must use drag in 2D ok!

avatar image Vardan Meliksetyan · Jan 16, 2014 at 10:27 PM 0
Share

After 30 $$anonymous$$utes or 1 hour I will write you !

avatar image Vardan Meliksetyan · Jan 16, 2014 at 11:14 PM 0
Share

lets tomorrow write and I will answer you

avatar image LuizEscobar · May 30, 2016 at 02:44 PM 0
Share

how can i use part of ur script to drag and throw objects in a 2D game, like mario picking up turtle shell...

avatar image Lanney · May 30, 2018 at 05:45 PM 0
Share

There's an error in line 30 if (spring.enabled = true) Should be if (spring.enabled == true) anyways Thank You very $$anonymous$$uch! :)

avatar image
1

Answer by Vardan Meliksetyan · Jan 17, 2014 at 08:28 AM

if( Input.GetMouseButtonDown(0))

     {
         Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
         RaycastHit hit;

         if( Physics.Raycast(ray, out hit) )
         {
             Debug.Log( hit.point );
             firstClickPosition = hit.point;
         }

// here your code "firstClickPosition is your click position"

     }
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
1

Answer by Keyserjaya99 · Oct 08, 2016 at 08:31 AM

void OnMouseDrag() {

  1. Vector3 MousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

  2. MousePos.z = 1f; //Optional

  3. transform.position = MousePos;

  4. }

void OnMouseUp() {

  1. Vector3 MousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);

  2. Vector2 dir = MousePos - transform.position;

  3. dir.Normalize ();

  4. GetComponent.velocity = dir * 10f;

  5. }

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 Wiseman17 · Jan 16, 2017 at 08:17 PM

I guess the most up-to-date answer is this one:

https://forum.unity3d.com/threads/realistic-2d-drag-drop-physics-like-in-the-little-inferno-game.366492/#post-2374339

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 davejones1 · Mar 19, 2018 at 03:35 PM

New Hi everyone, I am trying to make use of OnMouseDrag to limit the speed at which a UI slider can be moved. The slider is used to scrub through an animation. I want to limit the speed at which the UI slider can be moved so that the animation is played smoothly.

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

26 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

Related Questions

New Pointer Effector 2D Help 0 Answers

Gravity Direction Change 2D 1 Answer

2D swinging physics. 0 Answers

How to do time reversal with Unity2D / Physics2D? 1 Answer

How to convert Torque Stabilizer to RigidBody2D? 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