Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by nikoyal · Oct 05, 2017 at 10:39 PM · 2d gamedraggingcollsiondraggable

How to drag around an object and making it collide with other object 2D

Well i simply wanna make my sprite draggable with mouse/tap and colliding with other object's. So far i found alot of scripts on the internet to move my object around but i cant make it collide with enything and some of the tutorials about this are old.

So if u can send me to a proper tutorial that includes code or write me one i will probobly understand it since i cannot write my own :(

I will be very thankfull !

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
3
Best Answer

Answer by Cornelis-de-Jager · Oct 05, 2017 at 11:19 PM

Add the following the gameobject you want to drag. Create a scritp called Drag

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(BoxCollider))]
 
 public class Drag : MonoBehaviour {
     private Vector3 screenPoint;
     private Vector3 offset;
 
 void OnMouseDown() {
 
     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);
     Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
     transform.position =  Vector3.Lerp (transform.position, curPosition, Time.deltatime);
 }
 }




Comment
Add comment · Show 1 · 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 The_ZayNepz_Guy · 22 hours ago 0
Share

Hey, thanks a lot for the script.

I tried adding it but didn't get it working, but would there be a way to add a public float to control the speed of the object being dragged? Like, if I want some objects be feel heavier than others?

avatar image
0

Answer by ematsuno · Jan 25, 2019 at 06:54 PM

screenPoint is null on Mousedown, but you're on the right track.

Here's a simple version that does not account for the offset of the mouse-location and the object's offset:

  float camDistance;
 private void OnMouseDown()
 {
     camDistance = Vector3.Distance(transform.position, Camera.main.transform.position);
 }
 private void OnMouseDrag()
 {
     Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, camDistance);
     Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
     transform.position = Vector3.Lerp(transform.position, objPosition, Time.deltaTime);
 }


Comment
Add comment · Show 1 · 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 The_ZayNepz_Guy · 22 hours ago 0
Share

Hey, thanks a lot for the script.

I tried adding it but didn't get it working, but would there be a way to add a public float to control the speed of the object being dragged? Like, if I want some objects be feel heavier than others?

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

79 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

Related Questions

OnBeginDrag, OnEndDrag not being called for unknown reasons 1 Answer

How can I add constraints to a Rect Transform position? 0 Answers

Need Help on Obj Movement via Mouse (particular physics rulles) 1 Answer

Keep Draggable UI window within Canvas? 1 Answer

How can I drag a 2D object anywhere on the screen with the x axis? (touch) 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