• 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 sethuraj · Nov 28, 2013 at 10:20 AM · drag

How to drag gameobject elastically..?

Hi everyone,I need to click and drag a game object and when I release the game object it should go back to the original position with a wiggle effect like a bended rubber.

I know how to make the click and drag and bring it back to original state on releasing all I need is to get the wiggle effect due to elasticity and inertia with damping and the wiggle strength should depend on the stretch distance.Can anyone guide me through the scripting of this.

Below is the illustration which shows I want.

alt text

illustration.jpg (111.9 kB)
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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by OneCept-Games · Apr 04, 2018 at 09:41 PM

Why not use a Tween asset, like DOTween?

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 rupeshpamaihgari · Apr 04, 2018 at 06:00 PM

@sethuraj, the follow code works.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class dragScript : MonoBehaviour {
 
     private Vector3 screenPoint;
     private Vector3 offset;
 
     public Transform cube;
     public float speed;
     Vector3 startPosition;
     public bool CUbeReleased;
     public Vector3 dir;
     public Vector3 curPosition;
     public float value = 8.0f;
     void Start()
     {
         startPosition = transform.position;
     
     }
 
     void OnMouseDown()
     {
         screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
 
         offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
 
     }
 
     void OnMouseDrag()
     {
 
             Vector3 curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
 
             curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint) + offset;
             transform.position = curPosition;
             
     }
     void OnMouseUp()
     {
         
 
         CUbeReleased = true;
 
     }
 
     void Update() {
         
 
         if (CUbeReleased == true && Vector3.Distance(this.transform.position,cube.transform.position) > 0.3f) {
 
 
             dir = cube.transform.position - this.transform.position;
             dir.Normalize ();
 
             this.gameObject.GetComponent<Rigidbody> ().AddForce (dir * (Mathf.Sin (dir.sqrMagnitude)*value));
 
             //if (this.gameObject.GetComponent<Rigidbody> ().velocity.magnitude < 1.0f && Vector3.Distance(this.transform.position,cube.transform.position) < 0.2f) {
             
             //    value = 0.1f;
             //}
             Debug.Log (Mathf.Abs(Vector3.Distance(this.transform.position,cube.transform.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
0

Answer by Grixen · Feb 02, 2016 at 05:32 PM

Somehow for me it became a nontrivial task. Fully functional c# addition with smooth drag. For further users.

 private Vector3 origin;
 private float forceConstant = 20;
 private bool returning = false;
 private float precision = 0.1f;
 
 public float speed = 9.0f;
 private Vector3 targetPos;

 private void Start(){
     origin = transform.position;
 }
 
 private void Update(){

     if (Input.GetMouseButton (0)) {
                 
         float distance = transform.position.z - Camera.main.transform.position.z;
         targetPos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, distance);
         targetPos = Camera.main.ScreenToWorldPoint (targetPos);
         transform.position = Vector3.MoveTowards (transform.position, targetPos, speed * Time.deltaTime);

// transform.position = new Vector3 (Input.mousePosition.x / Screen.width, Input.mousePosition.y / Screen.width, 0);

     }

     if (Input.GetMouseButtonUp(0)){
         returning = true;
         GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
     }
     
     if (returning){
         GetComponent<Rigidbody>().AddForce (forceConstant*(origin - transform.position));
         GetComponent<Rigidbody>().velocity *= 0.9f;
         if (GetComponent<Rigidbody>().velocity.magnitude < precision && 
             Vector3.Distance(transform.position, origin) < precision){
             GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
             returning = false;
         }
     }
 }

}

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 MrVerdoux · Nov 28, 2013 at 10:36 AM

Whole new script, simpler but the concept remains.

It is known that elastic forces are of the type F = -k*x. But an ideal elastic force would never stop its movement so I added a perturbation that would slow down speed depending on its value. Right now its working and it´s very fun to see. Enjoy!

 using UnityEngine;
 using System.Collections;
 
 public class Draggable : MonoBehaviour{
 
     private Vector3 origin;
     private float forceConstant = 200;
     private bool returning = false;
     private float precision = 0.1f;
 
 
     private void Start(){
         origin = transform.position;
     }
     
     private void Update(){
         if (Input.GetMouseButton(0)){
             transform.position = new Vector3(5*Input.mousePosition.x/Screen.width, 5*Input.mousePosition.y/Screen.width, 0);
         }
         if (Input.GetMouseButtonUp(0)){
             returning = true;
                     rigidbody.constraints = RigidbodyConstraints.None;
         }
 
         if (returning){
             rigidbody.AddForce (forceConstant*(origin - transform.position));
             rigidbody.velocity *= 0.9f;
             if (rigidbody.velocity.magnitude < precision && 
                         Vector3.Distance(transform.position, origin) < precision){
                             rigidbody.constraints = RigidbodyConstraints.FreezeAll;
                 returning = false;
             }
         }
     }
 }

(Hopefully) Last edit: as you don´t seem to believe me I have uploaded a unity package with the script and a scene showing it. Just load, play and click. It works WELL, it only needs to improve the mouse input part, but your question wasn´t about it so I just implemented a fast solution.


draggable.unitypackage.zip (5.4 kB)
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 sethuraj · Nov 28, 2013 at 10:57 AM 0
Share

No this is not working.The UpdatePosition() function just brings the object smoothly back to its position.Its not making the object to wiggle before it stops.

avatar image sethuraj · Nov 28, 2013 at 11:55 AM 0
Share

sorry man just look at the code,there is no part in the code for making the wiggle.read my question.I need like a rubber effect.Click and drag and on release it should reach the original position like a stretched rubber band.It should offset left and right from its starting position like pingpong and come to rest.

avatar image MrVerdoux · Nov 28, 2013 at 02:30 PM 0
Share

Edited, it´s working well.

avatar image MFKJ · May 13, 2016 at 05:12 AM 0
Share

drag is not working correctly

avatar image piyushr99 · Nov 17, 2016 at 07:11 AM 0
Share

i downloaded package and used it in new project. Its not working.

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

21 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

Related Questions

Rotate object with rigidbody and high angular drag around particular local axis 0 Answers

Grab and throw object immediately 1 Answer

Drag an object from the start of a path to end 1 Answer

Can I force z position to match prefab when it's dragged onto scene? 1 Answer

Drag Question 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