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 Jammer3000 · Mar 03, 2014 at 02:35 PM · rotationjavascriptclick

How to rotate object on mouse click?

I have a project where when I click I set the gameObjects Z position to look up(0.09) and then if its not being clicked I make it wait 2 seconds and then set its position to look forward(0) again but for some reason everything I have tried doesn't work. It only works on the first click and once 2 seconds have passed sets it back to zero but will not set it to look up on any of the following clicks? Can someone please show me how they would do it in javascript if possible please?

 #pragma strict
 
 var rotationUp = 0.0f;
 var rotationDown = 0.0f;
 var rotationDownDelay = 0.0f;
 var jumpSound : AudioSource;
 function Update () {
         
         if (Input.GetButtonDown("Fire1")) {

             rigidbody2D.velocity = Vector2(0.0f, 7.0f);
             jumpSound.Play();
             gameObject.transform.rotation.z = rotationUp;
             LookDown();

         }
         
 }
 
 function LookDown () {
     
     yield WaitForSeconds (2);
     gameObject.transform.rotation.z = rotationDown;
     
 }
Comment
Add comment · Show 20
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 highpockets · Mar 03, 2014 at 03:39 PM 0
Share

You should post your script so someone can help. It's impossible to know where you went wrong without the script

avatar image Jammer3000 · Mar 03, 2014 at 04:14 PM 0
Share

Thanks for that highpockets I edited the question

avatar image highpockets · Mar 03, 2014 at 05:19 PM 0
Share

Right now, on every update that the button is down, you will be starting another LookDown(), which will wait for 2 seconds. That might be where your problem lies. I think you just want LookDown to happen once after you press the button. So, I think it would be better to do the LookDown() on button up, therefore, you guarantee only one LookDown() instance per button press.

On top of that, I would make a bool system to check the state of the LookDown() to ensure that an instance is not still in progress before calling it again. If it is in progress, stop the current one and start a new instance.

avatar image Jammer3000 · Mar 03, 2014 at 05:30 PM 0
Share

On button up seems to do the same thing which is it waits 2 seconds on the first click but the following ones only use a fraction of that delay. And if you could give me an example of that boolean system it'd be greatly appreciated

avatar image robertbu · Mar 03, 2014 at 05:41 PM 0
Share

Another problem (and perhaps the problem) is that you are directly assigning a single component of the Transform.rotation. The Transform.rotation is a Quaternion. You should not manipulate the components unless you understand Quaternions. You can use Transform.eulerAngles, but you need to assign all three angles. From the reference:

Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations. When setting them to a new value set them all at once

So on line 13, you can do:

 transform.eulerAngles = Vector3(0.0,0.0,90.0);

Then on line 13 you can do:

 transform.eulerAngles = Vector3(0.0,0.0,0.0);

Or you can do:

 transform.rotation = Quaternion.identity;
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ben-tmg · Mar 12, 2014 at 12:05 AM

Ok, I'll take a stab at it: (I haven't tested this)

I put in the suggestions from the other posters.

 #pragma strict
  
 var rotationUp = Vector3(0.0, 0.0, 90.0);
 var rotationDown = Vector3(0.0, 0.0, 0.0);
 var rotationDownDelay = 0.0f;
 var isLooking = false;
 var jumpSound : AudioSource;
 function Update () {
  
        if (Input.GetButtonUp("Fire1")) {
  
          //I assume this isn't related to the rotation stuff
          rigidbody2D.velocity = Vector2(0.0f, 7.0f);
          jumpSound.Play();
 
          if(isLooking == false)
          {
               isLooking = true;
               gameObject.transform.rotation = rotationUp;
               LookDown();
          }
  
        }
  
 }
  
 function LookDown () {
  
     yield WaitForSeconds (2);
     gameObject.transform.rotation = rotationDown;
     isLooking = false;
  
 }
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

25 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Z rotation locks to certain value randomly? 0 Answers

[SOLVED]Possible Alternation of Transform.Rotate 1 Answer

Click to destroy object 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