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
0
Question by aqib · Sep 25, 2013 at 12:25 PM · instantiatetranslateclonedraganddropauto

Auto Translate after cloning gameobject using Instantiate()

I want to create drag and drop functionality for a gameObject. When user touches the object it creates a duplicate and the duplicate supports drag and drop features when the original object remains in its position. When I apply it only on the original gameobject without creating duplicate it works fine but when I create the duplicate, both original object and duplicate start to translate automatically through y-axis. I cannot find the solution. please help out.

Here is my script for the gameObject:

 using UnityEngine;
 using System.Collections;

 public class DragableBrick : MonoBehaviour
 {
 
     bool isDragable = false;
     public GameObject replica;

     // Use this for initialization
     void Start ()
     {
         if (this.name != "Brick") {
             isDragable = true;
         }
     }
 
     // Update is called once per frame
     void Update ()
     {
 
     }
 
     void OnMouseDown(){
         if(isDragable == false){
             if(replica != null){
                 Instantiate(gameObject, this.transform.position, Quaternion.identity);
             }
         }
     }
     void OnMouseDrag(){
         if(isDragable == true){
             this.gameObject.transform.Translate (Event.current.delta.x * Time.deltaTime, -Event.current.delta.y * Time.deltaTime, 0);        
         }
     }    
 }
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

1 Reply

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

Answer by robertbu · Sep 25, 2013 at 01:41 PM

You have a ways to travel to get this code working correctly. Plus I'm not sure the behavior you are seeing is what you are describing (or at least the code above does not produce the behavior).

The really big issue is line 27. 'Event' is an GUI communication and is not valid outside of OnGUI. Even if it was, the values would be in GUI coordinates, not world coordinates.

Here is a script that is an approach to your problem. It assumes the camera is looking at positive 'Z'. The easiest way to test it is to create a new scene and then place the script on a cube.

 using UnityEngine;
 using System.Collections;
 
 public class DragableBrick : MonoBehaviour {
 
     private bool isDragable = false;    
     private Vector3 offset;
      
 
     void OnMouseDown(){
         var pos = Input.mousePosition;
         pos.z = transform.position.z - Camera.main.transform.position.z;
         pos = Camera.main.ScreenToWorldPoint (pos);
         offset = transform.position - pos;
         isDragable = true;
         Instantiate (gameObject);
     
     }
     void OnMouseDrag(){
         var pos = Input.mousePosition;
         pos.z = transform.position.z - Camera.main.transform.position.z;
         pos = Camera.main.ScreenToWorldPoint (pos);        
         
         transform.position = pos + offset;
     }
     
     void OnMouseUp() {
         isDragable = false;    
     }
 }

Note because of the way OnMouseDrag() works, what gets dragged is the original object, not the clone. But since they should be identical in everything but name (and that could easily be 'fixed'), hopefully this should not be a problem.

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 aqib · Sep 25, 2013 at 04:12 PM 0
Share

Thank you. :) problem solved. Had a rigid body added with the cube. Collision between original cube and clone caused the translate problem.

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

15 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

Related Questions

How can I get the x position for the left(and right) of the screen? 2 Answers

How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 1 Answer

Object reference not set to an instance of an object 1 Answer

What can I do to make the objects I instantiated move with the background? 1 Answer

Clone Selector 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