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
3
Question by KK · Nov 18, 2011 at 11:43 PM · dragdrop

Simple drag and drop - no physics

I want to simply drag and drop objects without rigidbody or physics. I found a old script of this written in C#. Its not working for me - I get a "NullReferenceException"

Can somebody fix this or convert it into Javascript? Here the code:

 using UnityEngine;
 
 using System.Collections;
 
 [RequireComponent(typeof(BoxCollider))]
 
 public class MovePoint2 : MonoBehaviour
 {
     private Vector3 screenPoint;
     private Vector3 offset;
 
     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));
 
         Screen.showCursor = false;
     }
 
     void OnMouseDrag() 
     { 
         Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
 
         Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
 
         transform.position = curPosition;
 
     }
 
     void OnMouseUp()
     {
         Screen.showCursor = true;
     }
 }


Here the Error in detail: UnityEngine.Camera.WorldToScreenPoint (Vector3 position) (at C:/BuildAgent/work/842f9557127e852/Runtime/ExportGenerated/Editor/UnityEngineCamera.cs:225) MovePoint2.OnMouseDown () (at Assets/Scripts/General Scripts/MovePoint2.cs:13) UnityEngine.SendMouseEvents:DoSendMouseEvents()

Comment
Add comment · Show 3
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 syclamoth · Nov 18, 2011 at 11:51 PM 0
Share

Do you have a camera tagged '$$anonymous$$ainCamera'?

avatar image KK · Nov 21, 2011 at 12:22 PM 0
Share

I forgot to tag the camera as "$$anonymous$$ain Camera". Now it does work well. Thanks!

avatar image benfattino · May 14, 2012 at 12:20 PM 0
Share

Is it possible convert this script in javascript?

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by benfattino · May 14, 2012 at 12:41 PM

Javascript:

 private var screenPoint: Vector3;
 private var offset: Vector3;
 private var curScreenPoint : Vector3;
 private var curPosition : Vector3;
     
 function Start () {
 }
 
 function Update () {
 }
 
 function 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));
     Screen.showCursor = false;
     }
     
 function OnMouseDrag() { 
     curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
     curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
     transform.position = curPosition;
     }
 
 function OnMouseUp(){
     Screen.showCursor = true;
     }

Tips welcome!

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 Mox.du · Nov 19, 2011 at 02:51 AM

Open new scene, make cube, add this script to cube (make sure you name script "MovePoint2" since this is the name of the class).

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 KK · Nov 21, 2011 at 12:23 PM 0
Share

See above. I forgot to tag the cam. Is working now. Thx

avatar image
0

Answer by dione001 · Nov 13, 2013 at 09:53 AM

What if there is a camera angle?. I want to move to object within the plane only,

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 Aarlangdi · Mar 07, 2014 at 03:18 PM

Check this out, this one has shown drop and drop in unity only in few lines of code --- http://aarlangdi.blogspot.com.au

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 elvira · Jun 02, 2015 at 08:51 PM 0
Share

Doesn't really looks like a drag and drop.. (a real one)

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

7 People are following this question.

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

Related Questions

Drag and drop an external file/folder into a running game window? 2 Answers

Drag item on screen once spawned. 0 Answers

How to move UI image between panels 0 Answers

Drag & Drop & Arrange 0 Answers

Drag and Drop Inside Editor 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