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 Beard · Sep 03, 2012 at 05:33 PM · mouserotatedragz axis

Mouse Controller

I have been trying to get a mouse controller to work for a 3D puzzle game. I need to drag prefabs around the screen, and I want to drag only in X Y coordinates by default with no rotation. While dragging I want to press W to enable a forward backward movement on the Z coordinate. Finally I want to be able to press Shift and enable only Object rotation. I have been working with the DragRigidBody.cs script that comes with Unity, I just don't know how to change to code to do what I want it to do.

Comment
Add comment · Show 2
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 AlucardJay · Sep 04, 2012 at 05:09 PM 0
Share

Have a look in the Unify Community website. There are ALOT of very handy scripts. Here is a link to the controller page : http://wiki.unity3d.com/index.php/Scripts/Controllers

avatar image AlucardJay · Sep 04, 2012 at 09:40 PM 0
Share

I had to edit your title, it was disturbing me =]

Am about to sign off, but shall have a look at your script tomorrow. I'm not very good with C# though.

For a better chance at getting an answer, convert your answer to a comment (hopefully it isnt too long), by clicking more > convert to comment . Then your question will show on the main page as having no answer, and hopefully attract someone to help =]

I am the same with my comments, I have to delete them all the time before passing my scripts to my colleague !

Definitely helps when writing/debugging =]

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Beard · Sep 04, 2012 at 09:22 PM

Thank you for the link! I was unable to find a script that suits my needs. I did find another script that I will need, and have not even got to yet. I have been experimenting, and found that if I work with the constraints on an object, I can set the constraints on key press, and the movement will be controlled using the DragRigidBody script. The only problem I am having is that I want the object to have constraints only while the mouse button is down. When I set constraints on mousedown it overrides everything other key press nested within the onmousedown. Each key press should change the constraints, and finally remove them when the item is dropped. (so gravity will act properly on the item) Here is my script. Sorry ahead of time for the amount of commenting, it is how I keep my projects strait so I work on only one thing at a time, and document nested statements.

This script will be placed on each rigidbody that I want to move with itself set as target in the inspector.

 using UnityEngine;
 using System.Collections;
 
 public class ObjectSelect : MonoBehaviour {
     
     
     public Transform _target;
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     
     #region Update
     void Update () {
     
         //Rotate Camera to LEFT side of container
         if(Input.GetKeyDown ("q")){
             //Will need to check to see what position camera is in.  
             //if MainCamera Xpos is >= _CameraCenterXPos{
             //MainCamera.Transform = 
             //Else MainCamera.Transform = CenterCameraXPos
             //        This should act as a TOGGLE, not a push and hold.
             
             //    Left side of cube will need renderer.enabled = false
             //    renderer.enabled = true when left and center.
             
         }
         
         //Rotate Camera to RIGHT side of container
         
         if(Input.GetKeyDown ("e")){
             // This key should operate the opposite of the Q button
             //
             //
             //            This will act as a camera TOGGLE not a push and hold.
             //
             //
             // Right side of cube will need renderer.enabled = false when on right
             //renderer.enabled = true when left and center.
             
             
         }
         
         
         if (Input.GetMouseButton(0)) { // if 1
         
             ConstraintsSetZRotate();
             
                 if(Input.GetKeyDown("w")){
                     //Code to control when W is pressed
                             
                         ConstraintsSetZon();            
                         Debug.Log ("W was pressed");
                     } //end W KeyDown Code
                         
                         
                 if(Input.GetKeyUp("w")){
                     //Code to reset values changed by W
 
                         ConstraintsSetZRotate();        
                         Debug.Log ("W was released");
                     }  // end W Keyup code                    
                         
                 if(Input.GetKeyDown("left shift")){
                     //Code to control when Shift is pressed
 
                         ConstraintsEnableRotateOnly();
                         Debug.Log("Left Shift Pressed");
                     }    //end Shift Keydown code
                         
                 if(Input.GetKeyUp("left shift")){
                     //Code to reset values changed by Shift
                                 
                         ConstraintsSetZRotate();
                         Debug.Log("Left Shift Released");                
                     } //end Shift Keyup Code
             
             
         } // End MouseDown event
         
         if(Input.GetMouseButtonUp(0)){
             
             ConstraintsZonly();
             
         }
         
         
     } // End Update
     #endregion
     
     private void ConstraintsSetZRotate(){
         //Sets All Rotation and Z axis to disabled.
         _target.rigidbody.constraints = RigidbodyConstraints.FreezeRotation|RigidbodyConstraints.FreezePositionZ;
     }
     
     private void ConstraintsSetZon(){
         //Disable all positions except Z
         _target.rigidbody.constraints = RigidbodyConstraints.FreezePositionX|RigidbodyConstraints.FreezePositionY|RigidbodyConstraints.FreezeRotation;
         
     }
     
     private void ConstraintsEnableRotateOnly(){
         //Disable all Positions Enable Only Rotations
         
         _target.rigidbody.constraints = RigidbodyConstraints.FreezePositionZ|RigidbodyConstraints.FreezePositionX|RigidbodyConstraints.FreezePositionY;    
     }
     
     private void ConstraintsZonly(){
         
         _target.rigidbody.constraints = RigidbodyConstraints.FreezePositionZ;
         
     }
 }
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

8 People are following this question.

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

Related Questions

C# rotating a 3D object left&right using mousedrag 0 Answers

Rotate on drag for IOS? 1 Answer

LookTo Rotation Mouse Drag 2 Answers

Bullet mouse control 2 Answers

Throwing an object (applying force to it when mouse is released) 2 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