• 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
1
Question by kuos · Sep 09, 2014 at 05:57 AM · mouseclick

Rotating a gameObject while MouseButton/Touch is down

Hi everyone, new to Unity. Hopefully I can format the code correctly on this site.

I am using C# and trying to code the object(Cube) to rotate quickly so long as the user has their finger still touching the object(programming for mobile input). I looked around a bit on the answers and some say that mouseclicks == single touch input?

I have two approaches in mind for this. 1) So long as the user has the mousebuttondown: rotate the cube; else: stop rotating

2) If the user clicks/taps once : rotate the cube a random angle, but choosing only from certain angles that show a FACE of the cube

My code for the first approach is as follows:

     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
         // rotate at 90 degrees per second
         if(Input.GetMouseButtonDown(0))
         {
             transform.Rotate(Vector3.up *Time.deltaTime*90);
         }
     }
 }

The above code only takes the mouse click input once so it rotates very little and I am suspecting Time.deltaTime*90 is not the brightest choice for the kind of rotation I want.

As for the second approach I still have a hard time figuring out how to rotate it by a random angle. I just started learning a bit of C#.

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 Jeric-Miana · Sep 09, 2014 at 06:41 AM 0
Share

Use Get$$anonymous$$ouseButton

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ThePersister · Sep 09, 2014 at 09:30 AM

Oh, this was actually a lot easier than I expected, haha. If you want to rotate as long a you have your MouseButtonDown, then, instead of using "GetMouseButtonDown(0)" (which only calls once per click) use "GetMouseButton(0)" (which keeps being entered for as long as the button is down)

Which gives you the code:

 void Update () {
     // rotate at 90 degrees per second
     if(Input.GetMouseButton(0))
     {
         transform.Rotate(Vector3.up *Time.deltaTime*90);
     }
 }

If you'd like a Random Rotation hur and thur, you can use Random.Range. Let's also add a RotationSpeed variable so you won't have to edit it in multiple different places. Here's some quick example code:

 public float RotationSpeed = 90f;
 
 void Update () {
     // Enter whilst the Left MouseButton is held down
     if(Input.GetMouseButton(0))
     {
         // Rotate over x, y and z with a speed between 0 and 40 * RotationSpeed
         // Warning: This will give some serious wacky rotating
         // You can get rid of some Axis if you want, turning them to 0, to not rotate over that Axis
         transform.Rotate(Random.Range(0,40) * Time.deltaTime * RotationSpeed,
                          Random.Range(0,40) * Time.deltaTime * RotationSpeed,
                          Random.Range(0,40) * Time.deltaTime * RotationSpeed);
     }
 }


I hope that helps, if you have any more specific questions, feel free to ask, otherwise, please accept this answer :)

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 kuos · Sep 09, 2014 at 03:44 PM 0
Share

Thank you, the random rotation is really interesting. I'm guessing 0,40 is the degree boundaries and the speed is so it isn't so slow since it is *Time.deltaTime. Will try this out. First one works perfectly though :)

avatar image ThePersister · Sep 17, 2014 at 01:53 PM 0
Share

Yepp, could you do me a favour and accept the answer so this could aid other people as well ;3

I hope you're making more progress with other Unity activities in the meantime ;)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can I change GUIButton behaviour? 1 Answer

Convert vector in world to mesh 1 Answer

Remove a part of image on mouse click ? 1 Answer

Activate gameobject upon mouseclick 1 Answer

mouse Down+Hold and double click events 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