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 darthbator · Jun 11, 2012 at 01:46 AM · dragmouse-draggrab

"Drag" gameobject around environment with mouse

So I was messing around trying to find a way to drag objects around the game environment with the mouse. I already have a real basic system in place to move character to a point that is clicked (using a raycast). I am trying to expand that basic model to take some different actions based on what object that ray hits. This is what I am woking with at the moment.

 void Update () {
     //When the Mouse is initially clicked do this
     if (Input.GetButtonDown("primaryMouse") && shootMouseRay(camRay, out rayHit)) {
         debugMi.text = rayHit.point.ToString(); //debug output
         //Check to see if this is an interaction object, if it is inspect the object
         if (rayHit.collider.gameObject.layer == interactionLayer) {
             switch (rayHit.collider.gameObject.tag) {
             case "Enemy" :
                 target = rayHit.collider.gameObject;
                 navAgent.MoveToGameObject(target, replan);
                 debugT.text = rayHit.collider.gameObject.ToString(); //debug output
                 break;
             case "drag" :
                 grabThing(rayHit.transform, camRay, rayHit);
                 break;
             }
         } else {
             target = null;
             debugT.text = ""; //debug output
         }
     }
     //When the Mouse is held do this if not in dragobj mode
     
     if (!dragMode && Input.GetButton("primaryMouse") && shootMouseRay(camRay, out rayHit)) {
         navAgent.MoveToPosition(rayHit.point, replan);
         debugMc.text = rayHit.point.ToString();
     }
 }
 
 //Method to shoot a ray from the camera "at" the mouse pointer
 bool shootMouseRay (Ray ray, out RaycastHit hit) {
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     return Physics.Raycast(ray, out hit);
 }
 
 //Grab an object and drag it around
 void grabThing (Transform objpos, Ray ray, RaycastHit hit) {
     if (Input.GetButton("primaryMouse")) {
         dragMode = true;
         if (shootMouseRay(ray, out hit)) {
             objpos.position = new Vector3(hit.point.x, objpos.position.y, hit.point.z);
         }
     }
     if (Input.GetButtonUp("primaryMouse")) {
         dragMode = false;
     }        
 }

Everything works pretty close to what I expect except for the dragging function. I have tried rewriting it a few ways. Basically I want to disable movement functions while dragging stuff and enter into my drag mode as long as a user holds a button down on the drag able gameObject. When the button is released I wanted to set my dragmode bool to false. This isn't what's occurring now.

When I click on any dragable object in my scene it will ever so slightly move the object over but it does not continue in dragmode as long as the mouse button is held down. Does this have to do with splitting it off into a separate method outside of update? Thanks a lot for any help!

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 flamy · Jun 14, 2012 at 12:06 PM 0
Share

http://unifycommunity.com/wiki/index.php?title=DragObject

1 Reply

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

Answer by Mortoc · Jun 11, 2012 at 01:54 AM

It sort of has to do with the external function. Generally you want to split things up in to functions exactly like you did, but I think it made you lose track of your logic flow.

The switch statement that's in Update is only hit on the frames where MouseButtonDown is true. When your function is called after that, GetButton will return true for that frame and GetButtonUp will never be hit.

For this problem (and any problem like it), throw in a bunch of Debug.Logs to see where the program flow goes to.

Comment
Add comment · Show 2 · 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 darthbator · Jun 14, 2012 at 11:46 AM 0
Share

So if I have something like

if (Input.GetButtonDown("mouse1")) { code }

all of the code in that case is scoped to only the first frame even if I nest some kind of general "GetButton" in there to check if it's being held down after the initial click was over a select able object?

avatar image Mortoc · Aug 03, 2012 at 10:34 PM 0
Share

Yes, GetButtonDown only returns true the frame the button went down. If you want a function that returns true every frame the button is held down, you want to use Input.GetButton

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

Collision with object using MouseDrag 1 Answer

Drag Camera Around an Object With Kinect (Official Plug-In) 0 Answers

Drag GameObject with mouse in just one axis 2 Answers

Click and Drag without rotation 2 Answers

Need help calculating offset when dragging an object 2D 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