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 /
This question was closed Nov 10, 2016 at 08:38 PM by omer1476 for the following reason:

old

avatar image
0
Question by omer1476 · May 24, 2016 at 01:43 AM · jointoverlapmouse-draggraboverlapping

Grab and drag object in physical and realistic way

Is there any method that I can grab object from any point on object and drag them realistic way. When I grab it, I want it to settle down by mass axis. Like this: alt text

Then when it's accelerating. It goes like this: alt text

When I tried to do this with springjoin2D, I cant make the same behaviour like above. Then, I did this with hinge joint but I can't avoid this object from overlapping with other object, is there any other way to do this? or Where did I make mistake here is my script.

here I tried to achive connected anchor to follow my mouse position. Object behavior like above but with one mistake, object can overlap collision area of other object.

I understand why this problem occur: Because I am teleporting object to mouse position so it ignores collision effect... But I cant figure out the solution

 using UnityEngine;
 using System.Collections;
 public class MouseJoint : MonoBehaviour {
     Vector2 mousePos2D;
     SpringJoint2D sj;
     void Start() {
         sj = gameObject.GetComponent<SpringJoint> ();
     }
     // Update is called once per frame
     void Update () {
         Vector3 mouseWorldPos3D = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         mousePos2D = new Vector2(mouseWorldPos3D.x, mouseWorldPos3D.y);
     }
     void FixedUpdate ()
     {    
         sj.connectedAnchor = mousePos2D;
     }
 }

I thing it is very fundemental to grab object like this, and drag it physically. I am very beginner to unity And sorry for my bad english

adsız.jpg (11.9 kB)
1.jpg (11.0 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

2 Replies

  • Sort: 
avatar image
1

Answer by saud_ahmed020 · May 24, 2016 at 07:37 AM

@omer1476 You can get this functionality by using new UI system. Just make a UI Canvas and make new image attach your sprite to this image. After this attach the following script to your image and register the methods that are in script.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 
 public class DragObject : MonoBehaviour {
     
     private Vector2 prevDragPos;
     
     public void OnBeginDrag(BaseEventData data) 
     {
         prevDragPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
     }
 
     public void OnDrag(BaseEventData data)
     {
         Vector3 curPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         
         
         float x = curPos.x - prevDragPos.x;
         float y = curPos.y - prevDragPos.y;
         
         Vector3 resPos = transform.position;
         resPos.x += x;
         resPos.y += y;
         transform.position = resPos;
         
         prevDragPos = curPos;
         
     }
 }
 


Comment
Add comment · Show 7 · 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 omer1476 · May 24, 2016 at 09:08 AM 0
Share

Thank you, I used new canvas, ı created new image to this canvas, I attached my sprite to this image as a source image. I added this script to this image. But I don't understand how I register method. Should I use another script for my rigidbody or should I use this method in another place?

alt text

121.jpg (72.1 kB)
avatar image saud_ahmed020 omer1476 · May 24, 2016 at 09:19 AM 1
Share

@omer1476 Add Event Trigger component to this Image and then register methods.

avatar image omer1476 saud_ahmed020 · May 24, 2016 at 10:06 AM 0
Share

What do you mean by "register method". Do you suggest me to subscribe this method to the event. Where should I register this methods, can you give me a simple example? I just read a book for c#, $$anonymous$$aybe I'm very beginner of c# too... I dont know the concept of "registering method". How do I learn this concept, how can I apply this in my project? Thank you again

Show more comments
Show more comments
avatar image DiegoSLTS · May 24, 2016 at 05:54 PM 0
Share

Does this really answers the question? Why is the answer marked as correct? There's nothing here using physics (Unity's or custom), so I can't imagine how dragging something will make it move like it's hanging from a point. It does work for dragging something, but that's just part of the question.

avatar image
0

Answer by Amanna · May 24, 2016 at 01:30 PM

you guys are awesome

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

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Text object set to "overflow" isn't scrolling vertically, new text just goes on top of existing text,Text object set to "overflow" seems to put new text on top of existing text, making it look "thicker" 0 Answers

Physics2D.OverlapBoxAll and Physics2D.OverlapAreaAll are inaccurate. 0 Answers

Preventing Spawn Overlap With Spheres 1 Answer

"Drag" gameobject around environment with mouse 1 Answer

Make colliding object not collide in overlapped area 0 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