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 /
  • Help Room /
This question was closed Aug 31, 2016 at 03:17 AM by Fontaine_Guo for the following reason:

I have solve it

avatar image
0
Question by Fontaine_Guo · Jul 27, 2016 at 04:52 PM · draggetmousebuttondown

How to create a prefab and then drag it by mouse

Hi , thank you for looking through this question. Now I want to init a gameobject which packed into a prefab on the position where I click and then drag it with my cursor. Because the object will be destoryed when I release the left mousebutton, so once I press the left mousebutton I'll init the gameobject and if I want to drag the button I can't release the mousebutton.

But now I can't let the gameobject go with the cursor , the following scripts have any mistakes to make it not work ?

Here is the script.NO1, this script will init a gameobject on the position where I clicked

 using UnityEngine;
 using System.Collections;
     
     public class CreateObjectAtPoint : MonoBehaviour
     {
         public Transform HelpObj;
         public GameObject clone;
         
         void Start()
         {
     
         }    
     
         public void FixedUpdate()
         {
             if(Input.GetMouseButtonDown(0))
             {
                 //Debug.Log("click");
                 Plane targetPlane = new Plane(Vector3.forward, 4.0f);
                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     
                 float distance = 0;
                 targetPlane.Raycast(ray, out distance);
     
                 Vector3 clonePos = ray.GetPoint(distance);
                 //  Debug.Log("x: " + clonePos.x + " y: " + clonePos.y + " z: " + clonePos.z);
                 clone = Instantiate(HelpObj, clonePos, Quaternion.identity) as GameObject;
             }
         }
     }

And there is script NO2, this scripts will destory the object I init when I release the left mousebutton and make the object go with cursor before I release the button:

  using UnityEngine;
  using DG.Tweening;
  using System.Collections;
     
     public class FingerMove : MonoBehaviour
     {
     
         private Plane m_TargetPlane;
         private float m_swipeSpeed = 0.1f;
         private bool isMove = true;
         public static float inputX;
         public static float inputY;
     
         void Awake()
         {
     
         }
     
         void Update()
         {
            if(Input.GetMouseButtonUp(0))
             {
                 Destroy(this.gameObject);
             }
     
         }
     
         IEnumerator OnMouseDown()
         {
     
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     
             Plane p = new Plane(Vector3.forward, 4.0f);
     
             float distance = 0;
     
             p.Raycast(ray, out distance);
     
             while (Input.GetMouseButton(0))
             {
                 Debug.Log("X: " + ray.GetPoint(distance).x + " Y: " + ray.GetPoint(distance).y + " Z:" + ray.GetPoint(distance).z);
                 this.gameObject.transform.position = ray.GetPoint(distance);
                 yield return new WaitForFixedUpdate();
             }
         }
     }
 

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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to Pick-up/Drag/Throw Rigidbodies? [3D] 0 Answers

Weird behaviour dragging a UI Image 0 Answers

Code help with mobile touch drag 3D Top Down 0 Answers

How to return back to false? 1 Answer

What is the best way to implement a more responsive custom drag event with Scroll Rect? 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