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 AT-Brackeys · Jan 25, 2013 at 11:18 PM · rotationobjectmouserotatingmousemove

Rotating object using mouse movement

I've been trying to code a "physics gun" from garrys mod into my game, though i can't seem to get the object rotating right. So i want the user to be able to hold down "e" when an object is grabbed to then rotate it using the mouse. Based on an earlier question (http://answers.unity3d.com/questions/10443/how-to-rotate-an-objects-x-y-z-based-on-mouse-move.html) i found out how to achieve this though it simply doesn't work for me. Nothing happens... This is a rough script i wrote to try it out, what am i doing wrong? :)

 #pragma strict
 var speed = 5.0f;
 
 var toolEnabled : String = "physicsGun";
 
 var objectSelectParticles : Transform;
 
 private var selectedObject : Transform;
 private var trackObject = false;
 
 function Update ()
 {
     if (toolEnabled == "physicsGun")
     {
         //Raycast for the object we wish to manipulate
         if (Input.GetMouseButtonDown(0))
         {
             var gunHit : RaycastHit;
             if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), gunHit) && gunHit.transform.tag == "Prop")
             {
                 //Enable the variable that is gonna alow for Update
                 trackObject = true;
                 //Make selectedObject date be = the data from raycast
                 selectedObject = gunHit.transform;
                 Instantiate(objectSelectParticles, gunHit.point, selectedObject.transform.rotation);
                 selectedObject.parent = transform;
                 //Make it kinematic
                 selectedObject.gameObject.rigidbody.isKinematic = true;
             }
         }
         
         if (trackObject == true)
         {
             //THIS IS WHERE I WANT THE ROTATING TO HAPPEN.
             if (Input.GetKeyDown(KeyCode.E))
             {
                 selectedObject.transform.Rotate(Vector3(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0) * Time.deltaTime * speed);
             }
             
             
             if (Input.GetAxis("Mouse ScrollWheel") < 0) // back
             {
                 selectedObject.transform.position = Vector3.MoveTowards(selectedObject.transform.position, transform.position, 5.0f*Time.deltaTime);
             }
         }
         
         //Turn everything off again
         if (Input.GetMouseButtonUp(0))
         {
             trackObject = false;
             if( selectedObject != null)
             {
                 selectedObject.parent = null;
                 selectedObject.gameObject.rigidbody.isKinematic = false;
             }
         }
     }
 }
 
 function ToolHandling (string)
 {
     Debug.Log("Tool = " + string);
     toolEnabled = string;
 }
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
0

Answer by robertbu · Jan 26, 2013 at 02:01 AM

Your problem is that you are using Input.GetKeyDown(). Use Input.GetKey() instead. GetKeyDown() only returns true in the frame that that the key was pressed.

Comment
Add comment · Show 4 · 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 AT-Brackeys · Jan 26, 2013 at 02:11 AM 0
Share

Whoops, a little mistake when i rewrote it. It is still not working though.

avatar image robertbu · Jan 26, 2013 at 05:01 AM 0
Share

I pulled the rotation code out into a separate script and it worked. Y rotation was backwards which could be fixed with a change in sign. At this point I'd either use the debugger or start sprinkling Debug.Log() statements to see the flow of the code. In particular is trackObject getting the value you think it should have.

avatar image AT-Brackeys · Jan 26, 2013 at 11:34 AM 0
Share

I guess i will start doing that, thank you for helping :)

avatar image AT-Brackeys · Jan 26, 2013 at 01:28 PM 0
Share

I've tried using the Debug.Log() but everything works as it should... The if statement gets called every frame... It must be something else.

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

10 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

Related Questions

Rotate a thrown spear to Mouse Position (2d top down game) 0 Answers

Smooth camera rotation on mobile and pc when GetMouseButton()? 0 Answers

Moving a object around with mouse, rotating it with arrow keys and placing it. 1 Answer

How to rotate an object around a fixed point so it follows the mouse cursor? 1 Answer

object rotation to another vector not changing 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