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 /
avatar image
0
Question by AlphaSpace · Mar 17, 2016 at 10:13 PM · unity 53ddragperspectivedrop

Drag 3d objects on a 3d scenario?

Im using a RTS style camera (55°), and I manage to do a click on a UI button to instantiate a prefab at my mouse position with this code.

             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             isInstantianting = false;
             if (Physics.Raycast(ray, out hit))
             {
                 Instantiate(initiateGO, hit.point, Quaternion.identity);
             }

And then I wanted to drag the prefab with my mouse, so I found this code here.

      using UnityEngine;
       
      public class DragTest2: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));
          }
       
          void OnMouseDrag() 
          {  
              Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
       
              Vector3 curPosition   = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
              transform.position = curPosition;
          }
      }


But what I really need is that the prefab follow my mouse without me having to hold a mouse button. Like ... I click the UI button to instantiate a prefab and then the prefab will be at the mouse position untill I click at the scenario to drop the prefab there, and I want the prefab to be always on top of the plane without going thru or high up to the sky whilst im dragging it.

Thx already!

Comment
Add comment · Show 1
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 AlphaSpace · Mar 21, 2016 at 01:01 PM 0
Share

bump?bump?

1 Reply

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

Answer by Christian-Lemos · Mar 21, 2016 at 11:25 PM

Hi

try this one:

 using UnityEngine;
 using System.Collections;
 
 public class UIInstantiate : MonoBehaviour 
 {
     public GameObject machineGun;
     GameObject instantiatedMachineGun;
 
     bool instanteateStep1;
     bool instanteateStep2;
     bool instanteateStep3;
 
     Vector3 wantedPos;
 
 
     void Start ()
     {
         instanteateStep1 = true;
         instanteateStep2 = false;
         instanteateStep3 = false;
 
     }
 
     void Update()
     {
         Step1 ();
         Step2 ();
         Step3 ();
     }
 
     void Step1()
     {
 
         //I used a key. You only need to adapt it to your UIbutton    
         if(Input.GetKeyDown(KeyCode.A) && instanteateStep1 == true)
         {
             RaycastHit hit1;
             Ray ray1 = Camera.main.ScreenPointToRay(Input.mousePosition);
 
             if(Physics.Raycast (ray1, out hit1, 100) )
             {
                 instantiatedMachineGun = (GameObject)Instantiate (machineGun, hit1.point, Quaternion.identity);
                 
                 instanteateStep1 = false;
                 instanteateStep2 = true;
             }
 
         }
     }
 
     void Step2()
     {
         RaycastHit hit2;
         Ray ray2 = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (instanteateStep2 == true) 
         {    
             if(Physics.Raycast (ray2, out hit2, 100))
             {
                 if(hit2.collider.gameObject == instantiatedMachineGun)
                 {
                     if(Input.GetButton ("Fire1"))
                     {
                         instanteateStep2 = false;
                         instanteateStep3 = true;
                     }
                 }    
                 
                 else
                 {
 
                     wantedPos = new Vector3  (hit2.point.x, hit2.point.y, hit2.point.z);
                     instantiatedMachineGun.transform.position = wantedPos;
     
                     if(Input.GetButton ("Fire1"))
                     {
                         instanteateStep2 = false;
                         instanteateStep3 = true;
                     }
                 }
             }
         }
     }
 
 
     void Step3()
     {
         if (instanteateStep3 == true) 
         {
             instanteateStep1 = true;
             instanteateStep3 = false;
             instantiatedMachineGun.transform.position = wantedPos;
         }
     }     
 }

I made and tested this script assigning it to an empty gameobject (like a gameController).

I'm sorry if this dont work for you.

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Drag and Drop with perspective camera 3D 1 Answer

Drag n Drop in a 3D orthographic environment 1 Answer

UI eventsystem Raycast Unity 5 does not recognize parent and child 1 Answer

How do i create multiple vertices from an object? 1 Answer

Creating 3d maze game 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